chainAsync - 链式调用异步函数

优质
小牛编辑
133浏览
2023-12-01

链式调用异步函数。

循环遍历包含异步事件的函数数组,每次异步事件完成后调用 next

const chainAsync = fns => {
  let curr = 0;
  const next = () => fns[curr++](next);
  next();
};
chainAsync([
  next => {
    console.log('0 seconds');
    setTimeout(next, 1000);
  },
  next => {
    console.log('1 second');
  }
]);