Skip to content

Instantly share code, notes, and snippets.

@shalithasuranga
Created October 22, 2019 15:49
Show Gist options
  • Save shalithasuranga/cf9ff49f721fb7f848267356ae4959ed to your computer and use it in GitHub Desktop.
Save shalithasuranga/cf9ff49f721fb7f848267356ae4959ed to your computer and use it in GitHub Desktop.
let getData = (url) => {
return new Promise((resolve, reject) => {
let data = {items: [{id: parseInt(Math.random() * 100000)},
{id: parseInt(Math.random() * 100000)}]};
console.log(1);
if(data.items[0].id % 4 != 0)
data.nextPage = url;
resolve(data);
});
}
let recursive = (url, A = []) => {
return new Promise((resolve, reject) => {
getData(url).then((data) => {
A = A.concat(data.items);
if(data.nextPage) {
resolve(recursive(data.nextPage, A));
}
else {
resolve(A);
}
});
});
}
recursive('sales/customers').then((data) => {
console.log(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment