Last active
January 24, 2017 00:04
-
-
Save solominh/e25e310ce40296edb31cb668f8942ce1 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 timer = (ms) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(); | |
}, ms) | |
}); | |
} | |
promises = [timer(3000), timer(5000), timer(1000), timer(2000)] | |
promises.reduce((chain, promise, idx) => { | |
console.log('Begin loop', idx) | |
return chain | |
.then(() => { | |
console.log('Return promise', idx) | |
return promise | |
}) | |
.then(() => { | |
console.log('Result', idx) | |
}) | |
}, Promise.resolve()) | |
// Same result: | |
Promise.resolve() | |
.then(() => { | |
console.log('Return promise', 0) | |
return promises[0] | |
}) | |
.then(() => { | |
console.log('Result', 0) | |
console.log() | |
}) | |
.then(() => { | |
console.log('Return promise', 1) | |
return promises[1] | |
}) | |
.then(() => { | |
console.log('Result', 1) | |
console.log() | |
}) | |
.then(() => { | |
console.log('Return promise', 2) | |
return promises[2] | |
}) | |
.then(() => { | |
console.log('Result', 2) | |
console.log() | |
}) | |
.then(() => { | |
console.log('Return promise', 3) | |
return promises[3] | |
}) | |
.then(() => { | |
console.log('Result', 3) | |
console.log() | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment