Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
Created July 13, 2018 14:04
Show Gist options
  • Save ben-bradley/59e28423194c6f8248f0c8ea549d6ba7 to your computer and use it in GitHub Desktop.
Save ben-bradley/59e28423194c6f8248f0c8ea549d6ba7 to your computer and use it in GitHub Desktop.
'use strict';
const iteratePromise = (promise, list) =>
list.reduce((promiseChain, item) =>
promiseChain.then((results) =>
promise(item).then((result) =>
results.concat(result)))
, Promise.resolve([]));
const list = [ `a`, `b`, `c` ];
const mockPromise = (n) =>
new Promise((resolve) => setTimeout(() => resolve({ n, timestamp: Date.now() }), 500));
iteratePromise(mockPromise, list)
.then((results) => console.log(`results ::`, results))
.catch((err) => console.log(`error ::`, err));
/*
results :: [ { n: 'a', timestamp: 1531490615421 },
{ n: 'b', timestamp: 1531490615923 },
{ n: 'c', timestamp: 1531490616424 } ]
*/
@ben-bradley
Copy link
Author

Sometimes Promise.all() is too much, this will force it to run serially.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment