Created
October 22, 2019 15:49
-
-
Save shalithasuranga/cf9ff49f721fb7f848267356ae4959ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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