Wednesday, 15 March 2017

node.js callback and recursion

I am using the google api to get a list of all the messages in my mailbox. The API is paginating the list, and with every call it returns the next page, so I have to call it recursively:

const fetch = (cb, next) => {
    google.gmail('v1').users.messages.list({
        auth: oauth2Client,
        userId: 'me',
        pageToken: next
    }, cb)
}
const store = (err, result) => {
    // do something with result and then
    if (result.nextPageToken) {
        fetch(store, result.nextPagetToken)
    }
}

fetch(store)

Is there a better way to do this to avoid the recursion so I don't bust the stack?



via 599644

No comments:

Post a Comment