Skip to content

Instantly share code, notes, and snippets.

@stenno
Last active June 6, 2019 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stenno/090b0bef0dfb986ca1e9ad171a8d5a88 to your computer and use it in GitHub Desktop.
Save stenno/090b0bef0dfb986ca1e9ad171a8d5a88 to your computer and use it in GitHub Desktop.
for-of vs. reduce for sequential promises
const urls = ['foo', 'bar','baz'];
// for-of
for (const url of urls) {
const result = await fetch(url);
console.log(result);
}
// reduce
urls.reduce((chain, url) => chain.then(fetch(url)), Promise.resolve()).forEach(result => console.log(result));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment