Created
February 3, 2019 21:33
-
-
Save johnz/92f010bd198315917da2a915a5460322 to your computer and use it in GitHub Desktop.
Recursive fetch api until no next url
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
// 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