Skip to content

Instantly share code, notes, and snippets.

@rook2pawn
Created August 14, 2018 11:45
Show Gist options
  • Save rook2pawn/4c77a0db8649b9692cf9404fef3d2809 to your computer and use it in GitHub Desktop.
Save rook2pawn/4c77a0db8649b9692cf9404fef3d2809 to your computer and use it in GitHub Desktop.
sequential resolution of promises
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