@cc-heart/utils
Version:
🔧 javascript common tools collection
27 lines (26 loc) • 656 B
text/typescript
/**
* Invokes a task queue.
*
* @param {Array<(...args: any) => any>} taskArray - An array of tasks to be executed.
* @return {Promise<void>} - A promise that resolves when all tasks are completed.
*/
export function invokeTaskQueue(
taskArray: Array<(...args: any) => any>,
): Promise<void> {
const taskQueue = taskArray.slice()
let index = 0
return new Promise((resolve) => {
const loopFunc = () => {
if (index >= taskQueue.length) {
resolve()
return
}
const fn = taskQueue[index++]
fn &&
Promise.resolve(fn?.()).finally(() => {
loopFunc()
})
}
loopFunc()
})
}