Skip to content

Instantly share code, notes, and snippets.

@markbiek
Created January 29, 2018 16:27
Show Gist options
  • Save markbiek/84c9d437d11a0d2b5ddf05c1e3c4ac52 to your computer and use it in GitHub Desktop.
Save markbiek/84c9d437d11a0d2b5ddf05c1e3c4ac52 to your computer and use it in GitHub Desktop.
function checkLogs(opts) {
// Initialize the array where we store our messages (only called the first time)
if (!opts.hasOwnProperty('messages')) {
opts.messages = [];
}
// Make the initial call to get the generator, specifying the url to get the first page of data
if (!opts.hasOwnProperty('logs')) {
opts.logs = getLogs(initialUrl);
}
let { logs, messages, onComplete } = opts;
let next = logs.next();
// Get the result of the most recent ajax call from the generator function
next.value.then(data => {
if (data) {
const {items, paging} = data;
// If we still have data, append it to our list of messages
messages = messages.concat(items);
// Then call ourselves again to get the results of the next ajax call
checkLogs({logs, messages, onComplete});
} else {
// If we don't have any more data, call our callback with the complete list of messages
onComplete(messages);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment