From 47decb44a21338393d0820e9a965bf27f22dfbcd Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Mon, 8 Jan 2024 20:51:54 +0330 Subject: [PATCH] feat(AsyncQueue): waitForFinish --- packages/async-queue/src/main.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/async-queue/src/main.ts b/packages/async-queue/src/main.ts index 6e1c4536..09d7f733 100644 --- a/packages/async-queue/src/main.ts +++ b/packages/async-queue/src/main.ts @@ -53,11 +53,13 @@ export class AsyncQueue { } setTimeout(() => { - task().then(flatomise.resolve, flatomise.reject).then(() => { - if (this.queue__[taskId] === flatomise.promise) { - delete this.queue__[taskId]; - } - }); + task() + .then(flatomise.resolve, flatomise.reject) + .then(() => { + if (this.queue__[taskId] === flatomise.promise) { + delete this.queue__[taskId]; + } + }); }, 0); return flatomise.promise; @@ -68,8 +70,28 @@ export class AsyncQueue { * * @param taskId task id * @returns true if the task is running, otherwise false. + * @example + * ```typescript + * if (queue.isRunning('longTaskId')) { + * // ... + * } + * ``` */ isRunning(taskId: string): boolean { return this.queue__[taskId] !== undefined; } + + /** + * Wait for the all tasks in the queue to finish. + * + * @param taskId task id + * @returns A promise that resolves when all tasks are done. + * @example + * ```typescript + * await queue.waitForFinish('longTaskId'); + * ``` + */ + waitForFinish(taskId: string): Promise { + return this.queue__[taskId] ?? Promise.resolve(); + } }