Created
August 14, 2018 11:45
-
-
Save rook2pawn/4c77a0db8649b9692cf9404fef3d2809 to your computer and use it in GitHub Desktop.
sequential resolution of 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
var x = new Promise ((resolve,reject) => { | |
setTimeout(() => { | |
resolve(10) | |
}, 200) | |
}) | |
var y = new Promise ((resolve,reject) => { | |
setTimeout(() => { | |
resolve(2) | |
}, 1000) | |
}) | |
var z = new Promise ((resolve,reject) => { | |
setTimeout(() => { | |
resolve(3) | |
}, 500) | |
}) | |
var list = [x,y,z]; | |
list.reduce((p, c) => { | |
return p.then((val) => { | |
console.log("intermediate val:", val); | |
return c | |
}) | |
}, Promise.resolve(0)) | |
.then((val) => { | |
console.log("final value:", val) | |
}) | |
// intermediate val: 0 | |
// intermediate val: 10 | |
// intermediate val: 2 | |
// final value: 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment