xxxxxxxxxx
async function slowCounter () {
const ret = await ([100, 200, 300, 400, 500]).reduce(
async (promise, wait, idx) => {
return promise.then(async last => {
const ret = last + wait
console.log(`${idx}: waiting ${wait}ms to return ${ret}`)
await new Promise((res, rej) => setTimeout(res, wait))
return ret
})
}, Promise.resolve(0))
console.log(ret)
}
slowCounter ()
xxxxxxxxxx
var store = [0, 1, 2, 3, 4];
var stored = store.reduce(function(pV, cV, cI){
console.log("pv: ", pV);
pV.push(cV);
return pV; // ********* Important ******
}, []);
xxxxxxxxxx
const sum = await [
Promise.resolve(1),
Promise.resolve(1),
Promise.resolve(1)
].reduce(async (previousPromise, itemPromise) => {
const sum = await previousPromise;
const item = await itemPromise;
return sum + item;
}, Promise.resolve(0))
// sum === 3