You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/promise-pool.md
+76Lines changed: 76 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -96,6 +96,82 @@ await PromisePool
96
96
```
97
97
98
98
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
+
constpool= 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
+
constpool= 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
+
constpool= 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
+
constpool= 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
+
99
175
## Error Handling
100
176
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.
0 commit comments