Skip to content

Commit d5c26c5

Browse files
committed
document onTaskStarted and onTaskFinished
1 parent 226b893 commit d5c26c5

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

packages/promise-pool.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,82 @@ await PromisePool
9696
```
9797

9898

99+
### Run Callbacks when a Task Starts
100+
You may use the `onTaskStarted` method to run a callback function as soon as an item in the promise pool started processing. The callback function receives two arguments:
101+
102+
1. the `item` for which the pool finished a task
103+
2. the `pool` instance allowing you to retrieve statistics
104+
105+
Here’s an `onTaskStarted` usage example:
106+
107+
```js
108+
const pool = PromisePool
109+
.for(users)
110+
.onTaskStarted((user, pool) => {
111+
// retrieve the number of currently active tasks in the pool
112+
pool.activeTaskCount()
113+
114+
// retrieve the percentage of processed tasks in the pool
115+
pool.processedPercentage()
116+
})
117+
```
118+
119+
You can add multiple callbacks by chaining the `onTaskStarted` calls. This allows you to separate the concerns of each function when you’re doing different things:
120+
121+
```js
122+
const pool = PromisePool
123+
.for(users)
124+
.onTaskStarted((user, pool) => {
125+
// update the progress bar with the processed using `pool.processedPercentage()`
126+
})
127+
.onTaskStarted((user, pool) => {
128+
// log the amount of currently processed items using pool.activeTaskCount()
129+
})
130+
```
131+
132+
```info
133+
**Notice:** the `onTaskStarted` callbacks don’t support async functions.
134+
```
135+
136+
137+
### Run Callbacks when a Task Finished
138+
You may use the `onTaskFinished` method to run a callback function as soon as an item in the pool finished processing. The callback function receives two arguments:
139+
140+
1. the `item` for which the pool finished a task
141+
2. the `pool` instance allowing you to retrieve statistics
142+
143+
Here’s an `onTaskFinished` usage example:
144+
145+
```js
146+
const pool = PromisePool
147+
.for(users)
148+
.onTaskFinished((user, pool) => {
149+
// retrieve the number of currently active tasks in the pool
150+
pool.activeTaskCount()
151+
152+
// retrieve the percentage of processed tasks in the pool
153+
pool.processedPercentage()
154+
})
155+
```
156+
157+
You can add multiple callbacks by chaining the `onTaskFinished` calls. This allows you to separate the concerns of each function when you’re doing different things:
158+
159+
```js
160+
const pool = PromisePool
161+
.for(users)
162+
.onTaskFinished((user, pool) => {
163+
// update the progress bar with the processed using `pool.processedPercentage()`
164+
})
165+
.onTaskFinished((user, pool) => {
166+
// log the amount of currently processed items using pool.activeTaskCount()
167+
})
168+
```
169+
170+
```info
171+
**Notice:** the `onTaskFinished` callbacks don’t support async functions.
172+
```
173+
174+
99175
## Error Handling
100176
The promise pool won’t throw errors while processing the items. It collects all errors and returns them after processing all items. You may then inspect the errors and handle them individually.
101177

0 commit comments

Comments
 (0)