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 爱生日祝福语简短独特