多个异步函数形成调用链

5年以前  |  阅读数:363 次  |  编程语言:JavaScript 

链异步函数。

循环遍历包含异步事件的函数数组, 当每个异步事件完成时调用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');  setTimeout(next, 1000); },
  next => { console.log('2 seconds'); }
])
*/

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8