[JS] 一行代码的 pipe

167 阅读1分钟

原始

const pipe = (init: Function, ...n: Function[]) => n.reduce((pre, cur) => cur(pre), init());

改进

  • pipe 果然还是应该支持异步呀
  • 第一个参数直接为一个值就可以了,写成方法还是有点绕
const pipe = async (init: any, ...fns: Function[]) => fns.reduce(async (pre, cur) => await cur(await pre), init);