Created
July 13, 2018 14:04
-
-
Save ben-bradley/59e28423194c6f8248f0c8ea549d6ba7 to your computer and use it in GitHub Desktop.
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
'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 } ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes
Promise.all()
is too much, this will force it to run serially.