Skip to content

Instantly share code, notes, and snippets.

@comountainclimber
Created January 10, 2017 03:46
Show Gist options
  • Save comountainclimber/5ace2c70ada36f9a2c6929e3090e04d9 to your computer and use it in GitHub Desktop.
Save comountainclimber/5ace2c70ada36f9a2c6929e3090e04d9 to your computer and use it in GitHub Desktop.
fun with promises
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