Last active
August 13, 2018 16:27
-
-
Save MicroBenz/ff69763cc0b0aa6f349c9cf85c3c174d to your computer and use it in GitHub Desktop.
Async Loop
This file contains hidden or 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
// Async version | |
async function test(){ | |
for(let i = 0;i < list.length(); i++){ | |
const data = await getData(i); | |
console.log(data); | |
} | |
} | |
await test(); | |
console.log('done'); | |
// Promise-based version | |
list | |
.reduce( | |
(prev, curr) => prev.then(() => getData(curr)), | |
Promise.resolve() | |
) | |
.then(() => console.log('done')); | |
// If order doesn't matter, just Promise.all() | |
Promise | |
.all(list.map(i => getData(i))) | |
.then(() => console.log('done')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment