/**
 * 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()
  })
}
