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 入住酒店文案祝福语简短