Skip to content

Instantly share code, notes, and snippets.

@MicroBenz
Last active August 13, 2018 16:27
Show Gist options
  • Save MicroBenz/ff69763cc0b0aa6f349c9cf85c3c174d to your computer and use it in GitHub Desktop.
Save MicroBenz/ff69763cc0b0aa6f349c9cf85c3c174d to your computer and use it in GitHub Desktop.
Async Loop
// 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