/*
异步函数队列
顺序执行数组的异步函数
*/
/**
*
* @param {Array:function} functions
*/
function chain(functions) {
// 递归 => functions[0](function(){functions[1](function(){functions[2](function(){functions[3](function(){})})})})
queue(functions, 0);
}
function queue(arr, i) {
if (i > arr.length - 1) return
arr[i](function () { queue(arr, i + 1) })
}
chain([
function (cb) { setTimeout(() => { console.log(1); cb(); }, 1000) },
function (cb) { setTimeout(() => { console.log(2); cb(); }, 1000) },
function (cb) { setTimeout(() => { console.log(3); cb(); }, 1000) },
function () { setTimeout(() => { console.log(4); }, 1000) }
])
js异步函数任务队列
最新推荐文章于 2023-11-20 16:00:25 发布