Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Last active October 29, 2017 03:33
Show Gist options
  • Save ngyuki/89463878a876715216ed23e0b2f144c9 to your computer and use it in GitHub Desktop.
Save ngyuki/89463878a876715216ed23e0b2f144c9 to your computer and use it in GitHub Desktop.
#!/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