Skip to content

Instantly share code, notes, and snippets.

@joelbarbosa
Last active September 27, 2017 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelbarbosa/4b3490ade6b3f5ae4f6adf9e335a589e to your computer and use it in GitHub Desktop.
Save joelbarbosa/4b3490ade6b3f5ae4f6adf9e335a589e to your computer and use it in GitHub Desktop.
Execute all promises in a chain.
var a = new Promise(res => setTimeout(()=>{
res(1)
}, 2000));
var b = new Promise(res => setTimeout(()=>{
res(2)
}, 1000));
var c = (fn) => new Promise(res => console.log(fn));
var arr = [a, b];
arr.reduce(function(promise, next) {
return promise.then(_ => next).then( value => {
console.log(value); // ***
return value;
});
}, Promise.resolve());
// => 1, 2
//hardcode
a.then(r1 => r1.then(c(r1).then(b.then(r2 => r2.then(c(r2))))))
// => 1, 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment