Created
August 14, 2018 12:25
-
-
Save rook2pawn/a465bf40f6d389c43ece3e9ab49eaa74 to your computer and use it in GitHub Desktop.
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 = function() { | |
return new Promise ((resolve,reject) => { | |
console.log("performing x"); | |
setTimeout(() => { | |
resolve(10) | |
}, 2000) | |
}) | |
} | |
var y = function() { | |
return new Promise ((resolve,reject) => { | |
console.log("performing y"); | |
setTimeout(() => { | |
resolve(2) | |
}, 2000) | |
}) | |
}; | |
var z = function() { | |
return new Promise ((resolve,reject) => { | |
console.log("performing z"); | |
setTimeout(() => { | |
resolve(3) | |
}, 2000) | |
}) | |
}; | |
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 | |
//performing x | |
//intermediate val: 10 | |
//performing y | |
//intermediate val: 2 | |
//performing z | |
//final value: 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment