Javascript :: pipeAsyncFunctions
对异步功能执行从左到右的功能组合。
与散布运算符()一起使用 可以执行从左到右的功能合成 。这些函数可以返回以下各项的组合:简单值, 也可以将它们定义为 通过返回的值 。所有函数必须是一元的。Array.prototype.reduce()...Promise.then()Promiseasyncawait
const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));
const sum = pipeAsyncFunctions(
x => x + 1,
x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),
x => x + 3,
async x => (await x) + 4
);
(async() => {
console.log(await sum(5)); //15(一秒钟后)
})();热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短