1const sum = await [
2 Promise.resolve(1),
3 Promise.resolve(1),
4 Promise.resolve(1)
5].reduce(async (previousPromise, itemPromise) => {
6 const sum = await previousPromise;
7 const item = await itemPromise;
8 return sum + item;
9}, Promise.resolve(0))
10
11// sum === 3
12
1async function slowCounter () {
2 const ret = await ([100, 200, 300, 400, 500]).reduce(
3 async (promise, wait, idx) => {
4 return promise.then(async last => {
5 const ret = last + wait
6 console.log(`${idx}: waiting ${wait}ms to return ${ret}`)
7 await new Promise((res, rej) => setTimeout(res, wait))
8 return ret
9 })
10 }, Promise.resolve(0))
11 console.log(ret)
12}
13
14slowCounter ()