const composeAsync = (...funcs) =>
arg => funcs.reduceRight(async (acc, fn) => fn(await acc), arg);
async function double(v) {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(v);
return v * 2;
}
composeAsync(double, double, double)(1)
.then(console.log, console.error);
- The reducer returns a Promise. So getting a value from the
acc
have to use theawait
keyword. - An initial value of the
acc
may be not a Promise. But it is not a problem, because theawait
keyword can accept a value other than a Promise.