-
-
Save sajadmsNew/8338e08e191b2dec71b0adc7bd5e79dd to your computer and use it in GitHub Desktop.
Promise waterfall
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
| function promiseChain(list, getPromise) { | |
| return list.reduce((accPromise, entry) => { | |
| return accPromise.then(acc => { | |
| return getPromise(entry).then(result => { | |
| return acc.concat(result); | |
| }); | |
| }); | |
| }, Promise.resolve([])); | |
| } | |
| function getPromise(entry) { | |
| return new Promise((resolve, reject) => { | |
| setTimeout(() => resolve(entry * 2), 100); | |
| }); | |
| } | |
| promiseChain([0,1,2], getPromise).then(result => console.dir(result)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment