Skip to content

Commit f8c89e6

Browse files
authored
Document poolTask.then() better (#277)
1 parent 9fa13af commit f8c89e6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

docs/usage-pool.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,19 @@ The pool comes with two methods that allow `await`-ing the completion of all tas
9797
The first one is `pool.completed()`. It returns a promise that resolves once all tasks have been executed and there are no more tasks left to run. If a task fails, the promise will be rejected.
9898

9999
The second one is `pool.settled()`. It also returns a promise that resolves when all tasks have been executed, but it will also resolve instead of reject if a task fails. The returned promise resolves to an array of errors.
100+
101+
As outlined before, pool tasks provide a Promise-like `.then()` method. You can use it to await the completion of a subset of a pool's queued tasks only.
102+
103+
```ts
104+
// (Created a pool and queued other pool tasks before…)
105+
106+
const myTasks: QueuedTask[] = []
107+
108+
for (let input = 0; input < 5; input++) {
109+
const task = pool.queue(worker => worker.work(input))
110+
myTasks.push(task)
111+
}
112+
113+
await Promise.all(myTasks)
114+
console.log("All worker.work() tasks have completed. Other pool tasks might still be running.")
115+
```

0 commit comments

Comments
 (0)