Last active
October 29, 2017 03:33
-
-
Save ngyuki/89463878a876715216ed23e0b2f144c9 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
#!/usr/bin/env node | |
// @see https://qiita.com/soarflat/items/1a9613e023200bbebcb3 | |
function sampleResolve(value) { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(value); | |
}, 1000); | |
}) | |
} | |
//function sample() { | |
// const array = [5, 10, 20]; | |
// return array.reduce((sum, value) => { | |
// return Promise.all([sum, sampleResolve(value)]) | |
// .then(([sum, value]) => sum + value * 2) | |
// }, Promise.resolve(0)); | |
//} | |
function sample() { | |
const array = [5, 10, 20]; | |
return array.reduce((sum, value) => { | |
return sum.then(sum => sampleResolve(value) | |
.then(value => sum + value * 2)) | |
}, Promise.resolve(0)); | |
} | |
sample().then((v) => { | |
console.log(v); // => 70 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment