Created
January 10, 2017 03:46
-
-
Save comountainclimber/5ace2c70ada36f9a2c6929e3090e04d9 to your computer and use it in GitHub Desktop.
fun with promises
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
const list = ['grape stomper', 'headband', 'purple urkle']; // collection that you want to perform async operation on | |
const results = []; | |
// series becomes an array of "thenable" Promises which have their | |
// succesfull results pushed into a seperate array | |
const series = list.map((item) => () => { | |
return Promise.resolve(asyncFunc(item)) | |
.then(result => results.push(result)); | |
}); | |
// "seed" the reduce function with a Promise resolution | |
// because every item in series is a promise we call then | |
series.reduce((prev, cur) => prev.then(cur), Promise.resolve()) | |
.then(() => ) // more async/handle success | |
.catch(error => ); // handle errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment