Skip to content

Instantly share code, notes, and snippets.

@rook2pawn
Created August 14, 2018 12:25
Show Gist options
  • Save rook2pawn/a465bf40f6d389c43ece3e9ab49eaa74 to your computer and use it in GitHub Desktop.
Save rook2pawn/a465bf40f6d389c43ece3e9ab49eaa74 to your computer and use it in GitHub Desktop.
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