Last active
September 27, 2017 17:06
-
-
Save joelbarbosa/4b3490ade6b3f5ae4f6adf9e335a589e to your computer and use it in GitHub Desktop.
Execute all promises in a chain.
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
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