Skip to content

Instantly share code, notes, and snippets.

@johnz
Created February 3, 2019 21:33
Show Gist options
  • Save johnz/92f010bd198315917da2a915a5460322 to your computer and use it in GitHub Desktop.
Save johnz/92f010bd198315917da2a915a5460322 to your computer and use it in GitHub Desktop.
Recursive fetch api until no next url
// Run in JsBin https://jsbin.com/gujemof/edit?js,console
let counter = 0;
const getUsersFromGraphApiFake = async (url) => {
counter++;
if (counter > 4) {
return {
users: [
{ name: 'userName', email: 'user@email.com' }
],
};
}
return {
users: [
{ name: 'userName', email: 'user@email.com' }
],
nextUrl: 'https://nexturl.com?offset=offset&limit=limit'
}
};
const fetchUsers = async (users, url) => {
if (!url) {
return users;
}
const result = await getUsersFromGraphApiFake(url);
users.push(result.users);
return await fetchUsers(users, result.nextUrl);
}
(async () => {
console.clear();
const users = await fetchUsers([], 'https://nexturl.com?offset=offset&limit=limit');
console.log('USERSs:', JSON.stringify(users, null, 2));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment