Skip to content

Commit

Permalink
feat(AsyncQueue): waitForFinish
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jan 8, 2024
1 parent 3193b43 commit 47decb4
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions packages/async-queue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<unknown> {
return this.queue__[taskId] ?? Promise.resolve();
}
}

0 comments on commit 47decb4

Please sign in to comment.