@handy-common-utils/promise-utils 1.7.0 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +926 -435
- package/dist/promise-utils.d.ts +54 -21
- package/dist/promise-utils.d.ts.map +1 -1
- package/dist/promise-utils.js +56 -24
- package/dist/promise-utils.js.map +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -110,242 +110,258 @@ await PromiseUtils.synchronized('my-lock', async () => {
|
|
|
110
110
|
|
|
111
111
|
## @handy-common-utils/promise-utils
|
|
112
112
|
|
|
113
|
-
###
|
|
113
|
+
### Classes
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
| Class | Description |
|
|
116
|
+
| ------ | ------ |
|
|
117
|
+
| [PromiseUtils](#classespromiseutilsmd) | - |
|
|
116
118
|
|
|
117
|
-
###
|
|
119
|
+
### Type Aliases
|
|
118
120
|
|
|
119
|
-
|
|
121
|
+
| Type Alias | Description |
|
|
122
|
+
| ------ | ------ |
|
|
123
|
+
| [PromiseStateType](#type-aliasespromisestatetypemd) | - |
|
|
120
124
|
|
|
121
125
|
### Variables
|
|
122
126
|
|
|
123
|
-
|
|
127
|
+
| Variable | Description |
|
|
128
|
+
| ------ | ------ |
|
|
129
|
+
| [cancellableDelayedReject](#variablescancellabledelayedrejectmd) | Creates a cancellable timer that will reject after a specified number of milliseconds. |
|
|
130
|
+
| [cancellableDelayedResolve](#variablescancellabledelayedresolvemd) | Creates a cancellable timer that will resolve after a specified number of milliseconds. |
|
|
131
|
+
| [delayedReject](#variablesdelayedrejectmd) | Creates a Promise that rejects after a specified number of milliseconds. |
|
|
132
|
+
| [delayedResolve](#variablesdelayedresolvemd) | Creates a Promise that resolves after a specified number of milliseconds. |
|
|
133
|
+
| [EXPONENTIAL\_SEQUENCE](#variablesexponential_sequencemd) | Array of 25 exponential numbers starting from 1 up to 33554432. It can be used to form your own backoff interval array. |
|
|
134
|
+
| [FIBONACCI\_SEQUENCE](#variablesfibonacci_sequencemd) | Array of 25 Fibonacci numbers starting from 1 up to 317811. It can be used to form your own backoff interval array. |
|
|
135
|
+
| [inParallel](#variablesinparallelmd) | Executes multiple jobs/operations in parallel. By default, all operations are executed regardless of any failures. In most cases, using withConcurrency might be more convenient. |
|
|
136
|
+
| [promiseState](#variablespromisestatemd) | Retrieves the state of the specified Promise. Note: The returned value is a Promise that resolves immediately. |
|
|
137
|
+
| [PromiseState](#variablespromisestate-1md) | The state of a Promise can only be one of: Pending, Fulfilled, and Rejected. |
|
|
138
|
+
| [repeat](#variablesrepeatmd) | Executes an operation repeatedly and collects all the results. This function is very useful for many scenarios, such like client-side pagination. |
|
|
139
|
+
| [runPeriodically](#variablesrunperiodicallymd) | Runs an operation periodically with configurable intervals and stopping conditions. |
|
|
140
|
+
| [synchronised](#variablessynchronisedmd) | This is just another spelling of synchronized. |
|
|
141
|
+
| [synchronized](#variablessynchronizedmd) | Provides mutual exclusion similar to synchronized in Java. Ensures no concurrent execution of any operation function associated with the same lock. The operation function has access to the state (when synchronized is called), settledState (when the operation function is called), and result (either the fulfilled result or the rejected reason) of the previous operation. If there is no previous invocation, state, settledState, and result will all be undefined. |
|
|
142
|
+
| [timeoutReject](#variablestimeoutrejectmd) | Applies a timeout to a Promise or a function that returns a Promise. If the timeout occurs, the returned Promise rejects with the specified reason. If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise. If the rejectReason parameter is a function and the timeout does not occur, the function will not be called. Note: The rejection of the operation parameter is not handled by this function. You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously." |
|
|
143
|
+
| [timeoutResolve](#variablestimeoutresolvemd) | Applies a timeout to a Promise or a function that returns a Promise. If the timeout occurs, the returned Promise resolves to the specified result. If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise. If the result parameter is a function and the timeout does not occur, the function will not be called. Note: The rejection of the operation parameter is not handled by this function. You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously." |
|
|
144
|
+
| [withConcurrency](#variableswithconcurrencymd) | Executes multiple jobs/operations with a specified level of concurrency. |
|
|
145
|
+
| [withRetry](#variableswithretrymd) | Repeatedly performs an operation until a specified criteria is met. |
|
|
124
146
|
|
|
125
|
-
|
|
147
|
+
## Classes
|
|
126
148
|
|
|
127
|
-
Array of 25 exponential numbers starting from 1 up to 33554432.
|
|
128
|
-
It can be used to form your own backoff interval array.
|
|
129
149
|
|
|
130
|
-
|
|
150
|
+
<a id="classespromiseutilsmd"></a>
|
|
131
151
|
|
|
132
|
-
|
|
133
|
-
// 1ms, 2ms, 4ms, 8ms, 16ms, 32ms
|
|
134
|
-
PromiseUtils.withRetry(() => doSomething(), EXPONENTIAL_SEQUENCE.slice(0, 5), err => err.statusCode === 429);
|
|
135
|
-
// 1s, 2s, 4s, 8s, 10s, 10s, 10s, 10s, 10s, 10s
|
|
136
|
-
PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(EXPONENTIAL_SEQUENCE[i], 10)), err => err.statusCode === 429);
|
|
137
|
-
// with +-10% randomness: 1s, 2s, 4s, 8s
|
|
138
|
-
PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 4).map(n => 1000 * n * (1 + (Math.random() - 0.5) / 5)), err => err.statusCode === 429);
|
|
139
|
-
```
|
|
152
|
+
### Abstract Class: PromiseUtils
|
|
140
153
|
|
|
141
|
-
|
|
154
|
+
#### Constructors
|
|
142
155
|
|
|
143
|
-
|
|
156
|
+
<a id="api-constructor"></a>
|
|
144
157
|
|
|
145
|
-
|
|
158
|
+
##### Constructor
|
|
146
159
|
|
|
147
|
-
|
|
148
|
-
It can be used to form your own backoff interval array.
|
|
160
|
+
> **new PromiseUtils**(): `PromiseUtils`
|
|
149
161
|
|
|
150
|
-
|
|
162
|
+
###### Returns
|
|
151
163
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
// 1s, 2s, 3s, 4s, 8s, 10s, 10s, 10s, 10s, 10s
|
|
156
|
-
PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(FIBONACCI_SEQUENCE[i], 10)), err => err.statusCode === 429);
|
|
157
|
-
// with +-10% randomness: 1s, 2s, 3s, 5s, 8s, 13s
|
|
158
|
-
PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 5).map(n => 1000 * n * (1 + (Math.random() - 0.5) / 5)), err => err.statusCode === 429);
|
|
159
|
-
```
|
|
164
|
+
`PromiseUtils`
|
|
165
|
+
|
|
166
|
+
#### Methods
|
|
160
167
|
|
|
161
|
-
|
|
168
|
+
<a id="api-cancellabledelayedreject"></a>
|
|
162
169
|
|
|
163
|
-
|
|
170
|
+
##### cancellableDelayedReject()
|
|
164
171
|
|
|
165
|
-
|
|
172
|
+
> `static` **cancellableDelayedReject**\<`T`, `R`\>(`ms`, `reason`): `object`
|
|
166
173
|
|
|
167
174
|
Creates a cancellable timer that will reject after a specified number of milliseconds.
|
|
168
175
|
|
|
169
176
|
The returned object contains:
|
|
170
|
-
- stop() to cancel the scheduled rejection (if called before the timer fires). Calling
|
|
171
|
-
stop() prevents the promise from being settled by this timer.
|
|
172
|
-
- promise which will reject with the supplied reason (or the value returned by the
|
|
173
|
-
reason function) after ms milliseconds unless stop() is called first.
|
|
177
|
+
- `stop()` to cancel the scheduled rejection (if called before the timer fires). Calling
|
|
178
|
+
`stop()` prevents the promise from being settled by this timer.
|
|
179
|
+
- `promise` which will reject with the supplied `reason` (or the value returned by the
|
|
180
|
+
`reason` function) after `ms` milliseconds unless `stop()` is called first.
|
|
174
181
|
|
|
175
|
-
If the reason is a PromiseLike that rejects, its rejection value will be used as the rejection reason.
|
|
182
|
+
If the `reason` is a PromiseLike that rejects, its rejection value will be used as the rejection reason.
|
|
176
183
|
|
|
177
|
-
|
|
184
|
+
###### Type Parameters
|
|
178
185
|
|
|
179
|
-
|
|
|
180
|
-
|
|
|
186
|
+
| Type Parameter | Default type |
|
|
187
|
+
| ------ | ------ |
|
|
181
188
|
| `T` | `never` |
|
|
182
189
|
| `R` | `any` |
|
|
183
190
|
|
|
184
|
-
|
|
191
|
+
###### Parameters
|
|
185
192
|
|
|
186
|
-
|
|
|
187
|
-
|
|
|
193
|
+
| Parameter | Type | Description |
|
|
194
|
+
| ------ | ------ | ------ |
|
|
188
195
|
| `ms` | `number` | The number of milliseconds after which the scheduled rejection will occur. |
|
|
189
196
|
| `reason` | `R` \| `PromiseLike`\<`R`\> \| () => `R` \| `PromiseLike`\<`R`\> | The reason for the rejection, or a function that supplies the reason. |
|
|
190
197
|
|
|
191
|
-
|
|
198
|
+
###### Returns
|
|
192
199
|
|
|
193
|
-
`
|
|
200
|
+
`object`
|
|
194
201
|
|
|
195
|
-
An object with stop() and promise
|
|
202
|
+
An object with `stop()` and `promise`.
|
|
196
203
|
|
|
197
204
|
| Name | Type |
|
|
198
|
-
|
|
|
205
|
+
| ------ | ------ |
|
|
199
206
|
| `promise` | `Promise`\<`T`\> |
|
|
200
|
-
| `stop` | () => `void` |
|
|
207
|
+
| `stop()` | () => `void` |
|
|
201
208
|
|
|
202
|
-
|
|
209
|
+
***
|
|
203
210
|
|
|
204
|
-
|
|
211
|
+
<a id="api-cancellabledelayedresolve"></a>
|
|
205
212
|
|
|
206
|
-
|
|
213
|
+
##### cancellableDelayedResolve()
|
|
214
|
+
|
|
215
|
+
> `static` **cancellableDelayedResolve**\<`T`\>(`ms`, `result?`): `object`
|
|
207
216
|
|
|
208
217
|
Creates a cancellable timer that will resolve after a specified number of milliseconds.
|
|
209
218
|
|
|
210
219
|
The returned object contains:
|
|
211
|
-
- stop() to cancel the scheduled resolution (if called before the timer fires). Calling
|
|
212
|
-
stop() prevents the promise from being settled by this timer.
|
|
213
|
-
- promise which will resolve with the supplied result (or the value returned by the
|
|
214
|
-
result function) after ms milliseconds unless stop() is called first.
|
|
220
|
+
- `stop()` to cancel the scheduled resolution (if called before the timer fires). Calling
|
|
221
|
+
`stop()` prevents the promise from being settled by this timer.
|
|
222
|
+
- `promise` which will resolve with the supplied `result` (or the value returned by the
|
|
223
|
+
`result` function) after `ms` milliseconds unless `stop()` is called first.
|
|
215
224
|
|
|
216
|
-
If the result is a
|
|
225
|
+
Note: If the `result` is a function that returns a Promise, the returned `promise` will
|
|
226
|
+
resolve with that Promise's resolution (i.e. it behaves like resolving with a PromiseLike).
|
|
217
227
|
|
|
218
|
-
|
|
228
|
+
###### Type Parameters
|
|
219
229
|
|
|
220
|
-
|
|
|
221
|
-
|
|
|
230
|
+
| Type Parameter |
|
|
231
|
+
| ------ |
|
|
222
232
|
| `T` |
|
|
223
233
|
|
|
224
|
-
|
|
234
|
+
###### Parameters
|
|
225
235
|
|
|
226
|
-
|
|
|
227
|
-
|
|
|
236
|
+
| Parameter | Type | Description |
|
|
237
|
+
| ------ | ------ | ------ |
|
|
228
238
|
| `ms` | `number` | The number of milliseconds after which the scheduled resolution will occur. |
|
|
229
239
|
| `result?` | `T` \| `PromiseLike`\<`T`\> \| () => `T` \| `PromiseLike`\<`T`\> | The result to be resolved by the Promise, or a function that supplies the result. |
|
|
230
240
|
|
|
231
|
-
|
|
241
|
+
###### Returns
|
|
232
242
|
|
|
233
|
-
`
|
|
243
|
+
`object`
|
|
234
244
|
|
|
235
|
-
An object with stop() and promise
|
|
245
|
+
An object with `stop()` and `promise`.
|
|
236
246
|
|
|
237
247
|
| Name | Type |
|
|
238
|
-
|
|
|
248
|
+
| ------ | ------ |
|
|
239
249
|
| `promise` | `Promise`\<`T`\> |
|
|
240
|
-
| `stop` | () => `void` |
|
|
250
|
+
| `stop()` | () => `void` |
|
|
251
|
+
|
|
252
|
+
***
|
|
241
253
|
|
|
242
|
-
|
|
254
|
+
<a id="api-delayedreject"></a>
|
|
243
255
|
|
|
244
|
-
|
|
256
|
+
##### delayedReject()
|
|
245
257
|
|
|
246
|
-
|
|
258
|
+
> `static` **delayedReject**\<`T`, `R`\>(`ms`, `reason`): `Promise`\<`T`\>
|
|
247
259
|
|
|
248
260
|
Creates a Promise that rejects after a specified number of milliseconds.
|
|
249
261
|
|
|
250
|
-
The reason argument may be:
|
|
262
|
+
The `reason` argument may be:
|
|
251
263
|
- a value to reject with,
|
|
252
264
|
- a PromiseLike whose rejection will be adopted by the returned Promise, or
|
|
253
265
|
- a function which is invoked when the timer fires and may return a value or a PromiseLike.
|
|
254
266
|
|
|
255
|
-
If reason is a function, it is called when the timer elapses; if it returns a Promise,
|
|
267
|
+
If `reason` is a function, it is called when the timer elapses; if it returns a Promise,
|
|
256
268
|
the returned Promise will reject with that Promise's rejection reason (or reject with the
|
|
257
269
|
returned value if it resolves).
|
|
258
270
|
|
|
259
|
-
|
|
271
|
+
###### Type Parameters
|
|
260
272
|
|
|
261
|
-
|
|
|
262
|
-
|
|
|
273
|
+
| Type Parameter | Default type |
|
|
274
|
+
| ------ | ------ |
|
|
263
275
|
| `T` | `never` |
|
|
264
276
|
| `R` | `any` |
|
|
265
277
|
|
|
266
|
-
|
|
278
|
+
###### Parameters
|
|
267
279
|
|
|
268
|
-
|
|
|
269
|
-
|
|
|
280
|
+
| Parameter | Type | Description |
|
|
281
|
+
| ------ | ------ | ------ |
|
|
270
282
|
| `ms` | `number` | The number of milliseconds after which the created Promise will reject. |
|
|
271
283
|
| `reason` | `R` \| `PromiseLike`\<`R`\> \| () => `R` \| `PromiseLike`\<`R`\> | The reason for the rejection, or a function that supplies the reason. |
|
|
272
284
|
|
|
273
|
-
|
|
285
|
+
###### Returns
|
|
274
286
|
|
|
275
287
|
`Promise`\<`T`\>
|
|
276
288
|
|
|
277
289
|
A Promise that rejects with the specified reason after the specified delay.
|
|
278
290
|
|
|
279
|
-
|
|
291
|
+
***
|
|
292
|
+
|
|
293
|
+
<a id="api-delayedresolve"></a>
|
|
280
294
|
|
|
281
|
-
|
|
295
|
+
##### delayedResolve()
|
|
282
296
|
|
|
283
|
-
|
|
297
|
+
> `static` **delayedResolve**\<`T`\>(`ms`, `result?`): `Promise`\<`T`\>
|
|
284
298
|
|
|
285
299
|
Creates a Promise that resolves after a specified number of milliseconds.
|
|
286
300
|
|
|
287
|
-
The result argument may be:
|
|
301
|
+
The `result` argument may be:
|
|
288
302
|
- a value to resolve with,
|
|
289
303
|
- a PromiseLike whose resolution will be adopted by the returned Promise, or
|
|
290
304
|
- a function which is invoked when the timer fires and may return a value or a PromiseLike.
|
|
291
305
|
|
|
292
|
-
If result is a function, it is called when the timer elapses; if it returns a Promise,
|
|
306
|
+
If `result` is a function, it is called when the timer elapses; if it returns a Promise,
|
|
293
307
|
the returned Promise will adopt that Promise's outcome.
|
|
294
308
|
|
|
295
|
-
|
|
309
|
+
###### Type Parameters
|
|
296
310
|
|
|
297
|
-
|
|
|
298
|
-
|
|
|
311
|
+
| Type Parameter |
|
|
312
|
+
| ------ |
|
|
299
313
|
| `T` |
|
|
300
314
|
|
|
301
|
-
|
|
315
|
+
###### Parameters
|
|
302
316
|
|
|
303
|
-
|
|
|
304
|
-
|
|
|
317
|
+
| Parameter | Type | Description |
|
|
318
|
+
| ------ | ------ | ------ |
|
|
305
319
|
| `ms` | `number` | The number of milliseconds after which the created Promise will resolve. |
|
|
306
320
|
| `result?` | `T` \| `PromiseLike`\<`T`\> \| () => `T` \| `PromiseLike`\<`T`\> | The result to be resolved by the Promise, or a function that supplies the result. |
|
|
307
321
|
|
|
308
|
-
|
|
322
|
+
###### Returns
|
|
309
323
|
|
|
310
324
|
`Promise`\<`T`\>
|
|
311
325
|
|
|
312
326
|
A Promise that resolves with the specified result after the specified delay.
|
|
313
327
|
|
|
314
|
-
|
|
328
|
+
***
|
|
329
|
+
|
|
330
|
+
<a id="api-inparallel"></a>
|
|
315
331
|
|
|
316
|
-
|
|
332
|
+
##### inParallel()
|
|
317
333
|
|
|
318
|
-
|
|
334
|
+
> `static` **inParallel**\<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`, `options?`): `Promise`\<(`Result` \| `TError`)[]\>
|
|
319
335
|
|
|
320
336
|
Executes multiple jobs/operations in parallel. By default, all operations are executed regardless of any failures.
|
|
321
|
-
In most cases, using withConcurrency might be more convenient.
|
|
337
|
+
In most cases, using [PromiseUtils.withConcurrency](#api-withconcurrency) might be more convenient.
|
|
322
338
|
|
|
323
339
|
By default, this function does not throw or reject an error when any job/operation fails.
|
|
324
340
|
Errors from operations are returned alongside results in the returned array.
|
|
325
341
|
This function only resolves when all jobs/operations are settled (either resolved or rejected).
|
|
326
342
|
|
|
327
|
-
If options.abortOnError is set to true, this function throws (or rejects with) an error immediately when any job/operation fails.
|
|
343
|
+
If `options.abortOnError` is set to true, this function throws (or rejects with) an error immediately when any job/operation fails.
|
|
328
344
|
In this mode, some jobs/operations may not be executed if one fails.
|
|
329
345
|
|
|
330
|
-
|
|
346
|
+
###### Type Parameters
|
|
331
347
|
|
|
332
|
-
|
|
|
333
|
-
|
|
|
334
|
-
| `Data` |
|
|
335
|
-
| `Result` |
|
|
336
|
-
| `TError` | `Result` |
|
|
348
|
+
| Type Parameter | Default type | Description |
|
|
349
|
+
| ------ | ------ | ------ |
|
|
350
|
+
| `Data` | - | The type of the job data, typically an Array. |
|
|
351
|
+
| `Result` | - | The type of the return value from the operation function. |
|
|
352
|
+
| `TError` | `Result` | The type for the error that could be thrown from the operation function, defaults to `Result`. |
|
|
337
353
|
|
|
338
|
-
|
|
354
|
+
###### Parameters
|
|
339
355
|
|
|
340
|
-
|
|
|
341
|
-
|
|
|
356
|
+
| Parameter | Type | Description |
|
|
357
|
+
| ------ | ------ | ------ |
|
|
342
358
|
| `parallelism` | `number` | The number of jobs/operations to run concurrently. |
|
|
343
359
|
| `jobs` | `Iterable`\<`Data`\> | The job data to be processed. This function can safely handle an infinite or unknown number of elements. |
|
|
344
|
-
| `operation` | (`job
|
|
345
|
-
| `options?` | `
|
|
346
|
-
| `options.abortOnError
|
|
360
|
+
| `operation` | (`job`, `index`) => `Promise`\<`Result`\> | The function that processes job data asynchronously. |
|
|
361
|
+
| `options?` | \{ `abortOnError`: `boolean`; \} | Options to control the function's behavior. |
|
|
362
|
+
| `options.abortOnError?` | `boolean` | If true, the function aborts and throws an error on the first failed operation. |
|
|
347
363
|
|
|
348
|
-
|
|
364
|
+
###### Returns
|
|
349
365
|
|
|
350
366
|
`Promise`\<(`Result` \| `TError`)[]\>
|
|
351
367
|
|
|
@@ -353,298 +369,460 @@ A promise that resolves to an array containing the results of the operations.
|
|
|
353
369
|
Each element is either a fulfilled result or a rejected error/reason.
|
|
354
370
|
The results or errors in the returned array are in the same order as the corresponding elements in the jobs array.
|
|
355
371
|
|
|
356
|
-
|
|
372
|
+
###### Example
|
|
357
373
|
|
|
358
|
-
|
|
374
|
+
```ts
|
|
375
|
+
// Capture errors in the returned array
|
|
376
|
+
const attributesAndPossibleErrors: Array<JobResult|JobError> = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => {
|
|
377
|
+
const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
|
|
378
|
+
return topicAttributes;
|
|
379
|
+
});
|
|
359
380
|
|
|
360
|
-
|
|
381
|
+
// Abort on the first error
|
|
382
|
+
let results: Array<JobResult>;
|
|
383
|
+
try {
|
|
384
|
+
results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true });
|
|
385
|
+
} catch (error) {
|
|
386
|
+
// handle the error
|
|
387
|
+
}
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
***
|
|
391
|
+
|
|
392
|
+
<a id="api-promisestate"></a>
|
|
393
|
+
|
|
394
|
+
##### promiseState()
|
|
395
|
+
|
|
396
|
+
> `static` **promiseState**(`p`): `Promise`\<`"Pending"` \| `"Fulfilled"` \| `"Rejected"`\>
|
|
361
397
|
|
|
362
398
|
Retrieves the state of the specified Promise.
|
|
363
399
|
Note: The returned value is a Promise that resolves immediately.
|
|
364
400
|
|
|
365
|
-
|
|
401
|
+
###### Parameters
|
|
366
402
|
|
|
367
|
-
|
|
|
368
|
-
|
|
|
403
|
+
| Parameter | Type | Description |
|
|
404
|
+
| ------ | ------ | ------ |
|
|
369
405
|
| `p` | `Promise`\<`any`\> | The Promise whose state is to be determined. |
|
|
370
406
|
|
|
371
|
-
|
|
407
|
+
###### Returns
|
|
372
408
|
|
|
373
|
-
`Promise
|
|
409
|
+
`Promise`\<`"Pending"` \| `"Fulfilled"` \| `"Rejected"`\>
|
|
374
410
|
|
|
375
411
|
A Promise that resolves immediately with the state of the input Promise.
|
|
376
412
|
|
|
377
|
-
|
|
413
|
+
***
|
|
414
|
+
|
|
415
|
+
<a id="api-repeat"></a>
|
|
378
416
|
|
|
379
|
-
|
|
417
|
+
##### repeat()
|
|
380
418
|
|
|
381
|
-
|
|
419
|
+
> `static` **repeat**\<`Result`, `Param`, `Collection`\>(`operation`, `nextParameter`, `collect`, `initialCollection`, `initialParameter`): `Promise`\<`Collection`\>
|
|
382
420
|
|
|
383
421
|
Executes an operation repeatedly and collects all the results.
|
|
384
422
|
This function is very useful for many scenarios, such like client-side pagination.
|
|
385
423
|
|
|
386
|
-
|
|
424
|
+
###### Type Parameters
|
|
387
425
|
|
|
388
|
-
|
|
|
389
|
-
|
|
|
390
|
-
| `Result` |
|
|
391
|
-
| `Param` |
|
|
392
|
-
| `Collection` |
|
|
426
|
+
| Type Parameter | Description |
|
|
427
|
+
| ------ | ------ |
|
|
428
|
+
| `Result` | The type of the operation result. |
|
|
429
|
+
| `Param` | The type of the input to the operation, typically a paging parameter. |
|
|
430
|
+
| `Collection` | The type of the collection returned by this function. |
|
|
393
431
|
|
|
394
|
-
|
|
432
|
+
###### Parameters
|
|
395
433
|
|
|
396
|
-
|
|
|
397
|
-
|
|
|
398
|
-
| `operation` | (`parameter
|
|
399
|
-
| `nextParameter` | (`response
|
|
400
|
-
| `collect` | (`collection
|
|
434
|
+
| Parameter | Type | Description |
|
|
435
|
+
| ------ | ------ | ------ |
|
|
436
|
+
| `operation` | (`parameter`) => `Promise`\<`Result`\> | A function that takes a parameter as input and returns a result. Typically, the parameter has optional fields to control paging. |
|
|
437
|
+
| `nextParameter` | (`response`) => `Partial`\<`Param`\> \| `Promise`\<`Partial`\<`Param`\>\> \| `null` | A function for calculating the next parameter from the operation result. Normally, this parameter controls paging. This function should return null when no further invocation of the operation function is desired. If further invocation is desired, the return value of this function can be a Promise or a non-Promise value. |
|
|
438
|
+
| `collect` | (`collection`, `result`) => `Collection` | A function for merging the operation result into the collection. |
|
|
401
439
|
| `initialCollection` | `Collection` | The initial collection, which will be the first argument passed to the first invocation of the collect function. |
|
|
402
440
|
| `initialParameter` | `Partial`\<`Param`\> | The parameter for the first operation. |
|
|
403
441
|
|
|
404
|
-
|
|
442
|
+
###### Returns
|
|
405
443
|
|
|
406
444
|
`Promise`\<`Collection`\>
|
|
407
445
|
|
|
408
446
|
A promise that resolves to a collection of all the results returned by the operation function.
|
|
409
447
|
|
|
410
|
-
|
|
448
|
+
###### Example
|
|
411
449
|
|
|
412
|
-
|
|
450
|
+
```ts
|
|
451
|
+
const domainNameObjects = await PromiseUtils.repeat(
|
|
452
|
+
pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(),
|
|
453
|
+
response => response.position? {position: response.position} : null,
|
|
454
|
+
(collection, response) => collection.concat(response.items!),
|
|
455
|
+
[] as APIGateway.DomainName[],
|
|
456
|
+
);
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
***
|
|
460
|
+
|
|
461
|
+
<a id="api-runperiodically"></a>
|
|
413
462
|
|
|
414
|
-
|
|
463
|
+
##### runPeriodically()
|
|
464
|
+
|
|
465
|
+
> `static` **runPeriodically**\<`T`\>(`operation`, `interval`, `options?`): `object`
|
|
415
466
|
|
|
416
467
|
Runs an operation periodically with configurable intervals and stopping conditions.
|
|
417
468
|
|
|
418
|
-
|
|
469
|
+
- `interval` may be a single number (ms), an array of numbers, or a function
|
|
470
|
+
that receives the iteration number (starting at 1) and returns the next
|
|
471
|
+
interval in milliseconds or `undefined` to stop.
|
|
472
|
+
- If the interval array runs out of elements or the function returns `undefined`
|
|
473
|
+
(or a negative value), no further invocations will be scheduled.
|
|
474
|
+
|
|
475
|
+
Options:
|
|
476
|
+
- `maxExecutions` stop after N runs (inclusive).
|
|
477
|
+
- `maxDurationMs` stop after elapsed ms since the first scheduled start.
|
|
478
|
+
- `schedule` controls how the interval is measured:
|
|
479
|
+
- `'delayAfterEnd'`: wait the interval after the previous operation completes
|
|
480
|
+
before scheduling the next one (equivalent to a fixed delay between ends).
|
|
481
|
+
- `'delayBetweenStarts'`: keep start times on a regular schedule (interval measured
|
|
482
|
+
between the starts of successive operations).
|
|
483
|
+
The default schedule is `'delayBetweenStarts'`.
|
|
419
484
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
485
|
+
Returns an object with `stop()` to cancel further executions and `done` which
|
|
486
|
+
resolves when the periodic runner stops. If the provided `operation` throws or
|
|
487
|
+
rejects, the `done` promise will reject with that error so callers can handle it.
|
|
488
|
+
|
|
489
|
+
Note: The first invocation of `operation` is scheduled after the first interval
|
|
490
|
+
elapses (i.e. this function does NOT call `operation` immediately). If you need
|
|
491
|
+
an immediate run, invoke `operation(1)` yourself before calling `runPeriodically`.
|
|
423
492
|
|
|
424
|
-
|
|
493
|
+
###### Type Parameters
|
|
425
494
|
|
|
426
|
-
|
|
|
427
|
-
|
|
|
428
|
-
| `
|
|
429
|
-
| `interval` | `number` \| `number`[] \| (`iteration`: `number`) => `undefined` \| `number` | The interval (ms), array of intervals, or function returning interval per iteration. |
|
|
430
|
-
| `options?` | `Object` | Options for maxExecutions, maxDurationMs, and schedule type. |
|
|
431
|
-
| `options.maxDurationMs?` | `number` | - |
|
|
432
|
-
| `options.maxExecutions?` | `number` | - |
|
|
433
|
-
| `options.schedule?` | ``"delayAfterEnd"`` \| ``"delayBetweenStarts"`` | - |
|
|
495
|
+
| Type Parameter | Description |
|
|
496
|
+
| ------ | ------ |
|
|
497
|
+
| `T` | The operation return type (ignored by the runner; used for typing). |
|
|
434
498
|
|
|
435
|
-
|
|
499
|
+
###### Parameters
|
|
436
500
|
|
|
437
|
-
|
|
501
|
+
| Parameter | Type | Description |
|
|
502
|
+
| ------ | ------ | ------ |
|
|
503
|
+
| `operation` | (`iteration`) => `T` \| `Promise`\<`T`\> | Function to run each iteration. Receives the iteration index (1-based). |
|
|
504
|
+
| `interval` | `number` \| `number`[] \| (`iteration`) => `number` \| `undefined` | Number | number[] | ((iteration: number) => number|undefined) defining waits. |
|
|
505
|
+
| `options?` | \{ `maxDurationMs?`: `number`; `maxExecutions?`: `number`; `schedule?`: `"delayAfterEnd"` \| `"delayBetweenStarts"`; \} | Optional configuration. |
|
|
506
|
+
| `options.maxDurationMs?` | `number` | Stop after N milliseconds. |
|
|
507
|
+
| `options.maxExecutions?` | `number` | Stop after N executions. |
|
|
508
|
+
| `options.schedule?` | `"delayAfterEnd"` \| `"delayBetweenStarts"` | How to measure intervals: `'delayAfterEnd'` or `'delayBetweenStarts'`. |
|
|
438
509
|
|
|
439
|
-
|
|
510
|
+
###### Returns
|
|
511
|
+
|
|
512
|
+
`object`
|
|
513
|
+
|
|
514
|
+
An object containing `stop()` to cancel further executions and `done` Promise
|
|
515
|
+
which resolves when the periodic runner stops (or rejects if the operation errors).
|
|
440
516
|
|
|
441
517
|
| Name | Type |
|
|
442
|
-
|
|
|
518
|
+
| ------ | ------ |
|
|
443
519
|
| `done` | `Promise`\<`void`\> |
|
|
444
|
-
| `stop` | () => `void` |
|
|
520
|
+
| `stop()` | () => `void` |
|
|
445
521
|
|
|
446
|
-
|
|
522
|
+
***
|
|
447
523
|
|
|
448
|
-
|
|
524
|
+
<a id="api-synchronised"></a>
|
|
449
525
|
|
|
450
|
-
|
|
526
|
+
##### synchronised()
|
|
451
527
|
|
|
452
|
-
|
|
528
|
+
> `static` **synchronised**\<`T`\>(`lock`, `operation`): `Promise`\<`T`\>
|
|
529
|
+
|
|
530
|
+
This is just another spelling of [PromiseUtils.synchronized](#api-synchronized).
|
|
453
531
|
|
|
454
|
-
|
|
532
|
+
###### Type Parameters
|
|
455
533
|
|
|
456
|
-
|
|
|
457
|
-
|
|
|
534
|
+
| Type Parameter |
|
|
535
|
+
| ------ |
|
|
458
536
|
| `T` |
|
|
459
537
|
|
|
460
|
-
|
|
538
|
+
###### Parameters
|
|
461
539
|
|
|
462
|
-
|
|
|
463
|
-
|
|
|
464
|
-
| `lock` | `any` | The object (such as a string, a number, or this in a class) used to identify the lock. |
|
|
465
|
-
| `operation` | (`previousState
|
|
540
|
+
| Parameter | Type | Description |
|
|
541
|
+
| ------ | ------ | ------ |
|
|
542
|
+
| `lock` | `any` | The object (such as a string, a number, or `this` in a class) used to identify the lock. |
|
|
543
|
+
| `operation` | (`previousState`, `previousSettledState`, `previousResult`) => `Promise`\<`T`\> | The function that performs the computation and returns a Promise. |
|
|
466
544
|
|
|
467
|
-
|
|
545
|
+
###### Returns
|
|
468
546
|
|
|
469
547
|
`Promise`\<`T`\>
|
|
470
548
|
|
|
471
549
|
The result of the operation function.
|
|
472
550
|
|
|
473
|
-
|
|
551
|
+
***
|
|
474
552
|
|
|
475
|
-
|
|
553
|
+
<a id="api-synchronized"></a>
|
|
476
554
|
|
|
477
|
-
|
|
555
|
+
##### synchronized()
|
|
478
556
|
|
|
479
|
-
|
|
557
|
+
> `static` **synchronized**\<`T`\>(`lock`, `operation`): `Promise`\<`T`\>
|
|
558
|
+
|
|
559
|
+
Provides mutual exclusion similar to `synchronized` in Java.
|
|
480
560
|
Ensures no concurrent execution of any operation function associated with the same lock.
|
|
481
|
-
The operation function has access to the state (when synchronized is called),
|
|
561
|
+
The operation function has access to the state (when `synchronized` is called),
|
|
482
562
|
settledState (when the operation function is called),
|
|
483
563
|
and result (either the fulfilled result or the rejected reason) of the previous operation.
|
|
484
564
|
If there is no previous invocation, state, settledState, and result will all be undefined.
|
|
485
565
|
|
|
486
|
-
|
|
566
|
+
###### Type Parameters
|
|
487
567
|
|
|
488
|
-
|
|
|
489
|
-
|
|
|
568
|
+
| Type Parameter |
|
|
569
|
+
| ------ |
|
|
490
570
|
| `T` |
|
|
491
571
|
|
|
492
|
-
|
|
572
|
+
###### Parameters
|
|
493
573
|
|
|
494
|
-
|
|
|
495
|
-
|
|
|
496
|
-
| `lock` | `any` | The object (such as a string, a number, or this in a class) used to identify the lock. |
|
|
497
|
-
| `operation` | (`previousState
|
|
574
|
+
| Parameter | Type | Description |
|
|
575
|
+
| ------ | ------ | ------ |
|
|
576
|
+
| `lock` | `any` | The object (such as a string, a number, or `this` in a class) used to identify the lock. |
|
|
577
|
+
| `operation` | (`previousState`, `previousSettledState`, `previousResult`) => `Promise`\<`T`\> | The function that performs the computation and returns a Promise. |
|
|
498
578
|
|
|
499
|
-
|
|
579
|
+
###### Returns
|
|
500
580
|
|
|
501
581
|
`Promise`\<`T`\>
|
|
502
582
|
|
|
503
583
|
The result of the operation function.
|
|
504
584
|
|
|
505
|
-
|
|
585
|
+
***
|
|
506
586
|
|
|
507
|
-
|
|
587
|
+
<a id="api-timeoutreject"></a>
|
|
508
588
|
|
|
509
|
-
|
|
589
|
+
##### timeoutReject()
|
|
590
|
+
|
|
591
|
+
> `static` **timeoutReject**\<`T`, `R`\>(`operation`, `ms`, `rejectReason`): `Promise`\<`T`\>
|
|
510
592
|
|
|
511
593
|
Applies a timeout to a Promise or a function that returns a Promise.
|
|
512
594
|
If the timeout occurs, the returned Promise rejects with the specified reason.
|
|
513
595
|
If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise.
|
|
514
|
-
If the rejectReason parameter is a function and the timeout does not occur, the function will not be called.
|
|
515
|
-
Note: The rejection of the operation parameter is not handled by this function. You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
596
|
+
If the `rejectReason` parameter is a function and the timeout does not occur, the function will not be called.
|
|
597
|
+
Note: The rejection of the `operation` parameter is not handled by this function. You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
516
598
|
|
|
517
|
-
|
|
599
|
+
###### Type Parameters
|
|
518
600
|
|
|
519
|
-
|
|
|
520
|
-
|
|
|
601
|
+
| Type Parameter | Default type |
|
|
602
|
+
| ------ | ------ |
|
|
521
603
|
| `T` | `never` |
|
|
522
604
|
| `R` | `any` |
|
|
523
605
|
|
|
524
|
-
|
|
606
|
+
###### Parameters
|
|
525
607
|
|
|
526
|
-
|
|
|
527
|
-
|
|
|
608
|
+
| Parameter | Type | Description |
|
|
609
|
+
| ------ | ------ | ------ |
|
|
528
610
|
| `operation` | `Promise`\<`T`\> \| () => `Promise`\<`T`\> | The original Promise or a function that returns a Promise to which the timeout will be applied. |
|
|
529
611
|
| `ms` | `number` | The number of milliseconds for the timeout. |
|
|
530
612
|
| `rejectReason` | `R` \| `PromiseLike`\<`R`\> \| () => `R` \| `PromiseLike`\<`R`\> | The reason to reject with if the timeout occurs, or a function that supplies the reason. |
|
|
531
613
|
|
|
532
|
-
|
|
614
|
+
###### Returns
|
|
533
615
|
|
|
534
616
|
`Promise`\<`T`\>
|
|
535
617
|
|
|
536
618
|
A new Promise that rejects with the specified reason if the timeout occurs.
|
|
537
619
|
|
|
538
|
-
|
|
620
|
+
***
|
|
539
621
|
|
|
540
|
-
|
|
622
|
+
<a id="api-timeoutresolve"></a>
|
|
541
623
|
|
|
542
|
-
|
|
624
|
+
##### timeoutResolve()
|
|
625
|
+
|
|
626
|
+
> `static` **timeoutResolve**\<`T`\>(`operation`, `ms`, `result?`): `Promise`\<`T`\>
|
|
543
627
|
|
|
544
628
|
Applies a timeout to a Promise or a function that returns a Promise.
|
|
545
629
|
If the timeout occurs, the returned Promise resolves to the specified result.
|
|
546
630
|
If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise.
|
|
547
|
-
If the result parameter is a function and the timeout does not occur, the function will not be called.
|
|
548
|
-
Note: The rejection of the operation parameter is not handled by this function.
|
|
631
|
+
If the `result` parameter is a function and the timeout does not occur, the function will not be called.
|
|
632
|
+
Note: The rejection of the `operation` parameter is not handled by this function.
|
|
549
633
|
You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
550
634
|
|
|
551
|
-
|
|
635
|
+
###### Type Parameters
|
|
552
636
|
|
|
553
|
-
|
|
|
554
|
-
|
|
|
637
|
+
| Type Parameter |
|
|
638
|
+
| ------ |
|
|
555
639
|
| `T` |
|
|
556
640
|
|
|
557
|
-
|
|
641
|
+
###### Parameters
|
|
558
642
|
|
|
559
|
-
|
|
|
560
|
-
|
|
|
643
|
+
| Parameter | Type | Description |
|
|
644
|
+
| ------ | ------ | ------ |
|
|
561
645
|
| `operation` | `Promise`\<`T`\> \| () => `Promise`\<`T`\> | The original Promise or a function that returns a Promise to which the timeout will be applied. |
|
|
562
646
|
| `ms` | `number` | The number of milliseconds for the timeout. |
|
|
563
647
|
| `result?` | `T` \| `PromiseLike`\<`T`\> \| () => `T` \| `PromiseLike`\<`T`\> | The result to resolve with if the timeout occurs, or a function that supplies the result. |
|
|
564
648
|
|
|
565
|
-
|
|
649
|
+
###### Returns
|
|
566
650
|
|
|
567
651
|
`Promise`\<`T`\>
|
|
568
652
|
|
|
569
653
|
A new Promise that resolves to the specified result if the timeout occurs.
|
|
570
654
|
|
|
571
|
-
|
|
655
|
+
***
|
|
572
656
|
|
|
573
|
-
|
|
657
|
+
<a id="api-withconcurrency"></a>
|
|
574
658
|
|
|
575
|
-
|
|
659
|
+
##### withConcurrency()
|
|
660
|
+
|
|
661
|
+
> `static` **withConcurrency**\<`Data`, `Result`\>(`concurrency`, `jobs`, `operation`): `Promise`\<`Result`[]\>
|
|
576
662
|
|
|
577
663
|
Executes multiple jobs/operations with a specified level of concurrency.
|
|
578
664
|
|
|
579
|
-
|
|
665
|
+
Unlike `inParallel(...)`, this function may throw or reject an error when a job/operation fails.
|
|
666
|
+
When an error is re-thrown, remaining operations will not be executed.
|
|
667
|
+
If you want all the operations to always be executed, use [PromiseUtils.inParallel](#api-inparallel) instead.
|
|
580
668
|
|
|
581
|
-
|
|
582
|
-
| :------ |
|
|
583
|
-
| `Data` |
|
|
584
|
-
| `Result` |
|
|
669
|
+
###### Type Parameters
|
|
585
670
|
|
|
586
|
-
|
|
671
|
+
| Type Parameter | Description |
|
|
672
|
+
| ------ | ------ |
|
|
673
|
+
| `Data` | The type of the job data, typically an Array. |
|
|
674
|
+
| `Result` | The type of the return value from the operation function. |
|
|
587
675
|
|
|
588
|
-
|
|
589
|
-
|
|
676
|
+
###### Parameters
|
|
677
|
+
|
|
678
|
+
| Parameter | Type | Description |
|
|
679
|
+
| ------ | ------ | ------ |
|
|
590
680
|
| `concurrency` | `number` | The number of jobs/operations to run concurrently. |
|
|
591
681
|
| `jobs` | `Iterable`\<`Data`\> | The job data to be processed. This function can handle an infinite or unknown number of elements safely. |
|
|
592
|
-
| `operation` | (`job
|
|
682
|
+
| `operation` | (`job`, `index`) => `Promise`\<`Result`\> | The function that processes job data asynchronously. |
|
|
593
683
|
|
|
594
|
-
|
|
684
|
+
###### Returns
|
|
595
685
|
|
|
596
686
|
`Promise`\<`Result`[]\>
|
|
597
687
|
|
|
598
688
|
A promise that resolves to an array containing the results from the operation function.
|
|
599
689
|
The results in the returned array are in the same order as the corresponding elements in the jobs array.
|
|
600
690
|
|
|
601
|
-
|
|
691
|
+
###### Example
|
|
692
|
+
|
|
693
|
+
```ts
|
|
694
|
+
// At any time, there would be no more than 5 concurrency API calls. Error would be re-thrown immediately when it occurs.
|
|
695
|
+
const attributes = await PromiseUtils.withConcurrency(5, topicArns, async (topicArn) => {
|
|
696
|
+
const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
|
|
697
|
+
return topicAttributes;
|
|
698
|
+
});
|
|
699
|
+
```
|
|
700
|
+
|
|
701
|
+
***
|
|
602
702
|
|
|
603
|
-
|
|
703
|
+
<a id="api-withretry"></a>
|
|
604
704
|
|
|
605
|
-
|
|
705
|
+
##### withRetry()
|
|
706
|
+
|
|
707
|
+
> `static` **withRetry**\<`Result`, `TError`\>(`operation`, `backoff`, `shouldRetry`): `Promise`\<`Result`\>
|
|
606
708
|
|
|
607
709
|
Repeatedly performs an operation until a specified criteria is met.
|
|
608
710
|
|
|
609
|
-
|
|
711
|
+
###### Type Parameters
|
|
610
712
|
|
|
611
|
-
|
|
|
612
|
-
|
|
|
613
|
-
| `Result` |
|
|
614
|
-
| `TError` | `any` |
|
|
713
|
+
| Type Parameter | Default type | Description |
|
|
714
|
+
| ------ | ------ | ------ |
|
|
715
|
+
| `Result` | - | Type of the operation result. |
|
|
716
|
+
| `TError` | `any` | Type of the possible error that could be generated by the operation. |
|
|
615
717
|
|
|
616
|
-
|
|
718
|
+
###### Parameters
|
|
617
719
|
|
|
618
|
-
|
|
|
619
|
-
|
|
|
620
|
-
| `operation` | (`attempt
|
|
621
|
-
| `backoff` | `number`[] \| (`attempt
|
|
622
|
-
| `shouldRetry` | (`previousError
|
|
720
|
+
| Parameter | Type | Description |
|
|
721
|
+
| ------ | ------ | ------ |
|
|
722
|
+
| `operation` | (`attempt`, `previousResult`, `previousError`) => `Promise`\<`Result`\> | A function that outputs a Promise result. Typically, the operation does not use its arguments. |
|
|
723
|
+
| `backoff` | `number`[] \| (`attempt`, `previousResult`, `previousError`) => `number` \| `undefined` | An array of retry backoff periods (in milliseconds) or a function for calculating them. If retry is desired, the specified backoff period is waited before the next call to the operation. If the array runs out of elements or the function returns `undefined` or a negative number, no further calls to the operation will be made. The `attempt` argument passed to the backoff function starts from 1, as it is called immediately after the first attempt and before the first retry. |
|
|
724
|
+
| `shouldRetry` | (`previousError`, `previousResult`, `attempt`) => `boolean` | A predicate function for deciding whether another call to the operation should occur. If this argument is not defined, a retry will occur whenever the operation rejects with an error. The `shouldRetry` function is evaluated before the `backoff`. The `attempt` argument passed to the shouldRetry function starts from 1. |
|
|
623
725
|
|
|
624
|
-
|
|
726
|
+
###### Returns
|
|
625
727
|
|
|
626
728
|
`Promise`\<`Result`\>
|
|
627
729
|
|
|
628
730
|
A promise of the operation result, potentially with retries applied.
|
|
629
731
|
|
|
630
|
-
|
|
732
|
+
###### Example
|
|
631
733
|
|
|
734
|
+
```ts
|
|
735
|
+
const result = await PromiseUtils.withRetry(() => doSomething(), [100, 200, 300, 500, 800, 1000]);
|
|
736
|
+
const result2 = await PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(FIBONACCI_SEQUENCE[i], 10), err => err.statusCode === 429);
|
|
737
|
+
const result3 = await PromiseUtils.withRetry(() => doSomething(), attempt => attempt <= 8 ? 1000 * Math.min(FIBONACCI_SEQUENCE[attempt - 1], 10) : undefined, err => err.statusCode === 429);
|
|
738
|
+
```
|
|
632
739
|
|
|
633
|
-
|
|
740
|
+
## Type Aliases
|
|
634
741
|
|
|
635
|
-
### Class: PromiseUtils
|
|
636
742
|
|
|
637
|
-
|
|
743
|
+
<a id="type-aliasespromisestatetypemd"></a>
|
|
638
744
|
|
|
639
|
-
|
|
745
|
+
### Type Alias: PromiseStateType
|
|
640
746
|
|
|
641
|
-
|
|
747
|
+
> **PromiseStateType** = keyof *typeof* [`PromiseState`](#variablespromisestate-1md)
|
|
748
|
+
|
|
749
|
+
## Variables
|
|
642
750
|
|
|
643
|
-
#### Methods
|
|
644
751
|
|
|
645
|
-
|
|
752
|
+
<a id="variablesexponential_sequencemd"></a>
|
|
646
753
|
|
|
647
|
-
|
|
754
|
+
### Variable: EXPONENTIAL\_SEQUENCE
|
|
755
|
+
|
|
756
|
+
> `const` **EXPONENTIAL\_SEQUENCE**: `number`[]
|
|
757
|
+
|
|
758
|
+
Array of 25 exponential numbers starting from 1 up to 33554432.
|
|
759
|
+
It can be used to form your own backoff interval array.
|
|
760
|
+
|
|
761
|
+
#### Example
|
|
762
|
+
|
|
763
|
+
```ts
|
|
764
|
+
// 1ms, 2ms, 4ms, 8ms, 16ms, 32ms
|
|
765
|
+
PromiseUtils.withRetry(() => doSomething(), EXPONENTIAL_SEQUENCE.slice(0, 5), err => err.statusCode === 429);
|
|
766
|
+
// 1s, 2s, 4s, 8s, 10s, 10s, 10s, 10s, 10s, 10s
|
|
767
|
+
PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(EXPONENTIAL_SEQUENCE[i], 10)), err => err.statusCode === 429);
|
|
768
|
+
// with +-10% randomness: 1s, 2s, 4s, 8s
|
|
769
|
+
PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 4).map(n => 1000 * n * (1 + (Math.random() - 0.5) / 5)), err => err.statusCode === 429);
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
<a id="variablesfibonacci_sequencemd"></a>
|
|
774
|
+
|
|
775
|
+
### Variable: FIBONACCI\_SEQUENCE
|
|
776
|
+
|
|
777
|
+
> `const` **FIBONACCI\_SEQUENCE**: `number`[]
|
|
778
|
+
|
|
779
|
+
Array of 25 Fibonacci numbers starting from 1 up to 317811.
|
|
780
|
+
It can be used to form your own backoff interval array.
|
|
781
|
+
|
|
782
|
+
#### Example
|
|
783
|
+
|
|
784
|
+
```ts
|
|
785
|
+
// 1ms, 2ms, 3ms, 5ms, 8ms, 13ms
|
|
786
|
+
PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 5), err => err.statusCode === 429);
|
|
787
|
+
// 1s, 2s, 3s, 4s, 8s, 10s, 10s, 10s, 10s, 10s
|
|
788
|
+
PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(FIBONACCI_SEQUENCE[i], 10)), err => err.statusCode === 429);
|
|
789
|
+
// with +-10% randomness: 1s, 2s, 3s, 5s, 8s, 13s
|
|
790
|
+
PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 5).map(n => 1000 * n * (1 + (Math.random() - 0.5) / 5)), err => err.statusCode === 429);
|
|
791
|
+
```
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
<a id="variablespromisestate-1md"></a>
|
|
795
|
+
|
|
796
|
+
### Variable: PromiseState
|
|
797
|
+
|
|
798
|
+
> `const` **PromiseState**: `object`
|
|
799
|
+
|
|
800
|
+
The state of a Promise can only be one of: Pending, Fulfilled, and Rejected.
|
|
801
|
+
|
|
802
|
+
#### Type Declaration
|
|
803
|
+
|
|
804
|
+
| Name | Type | Default value |
|
|
805
|
+
| ------ | ------ | ------ |
|
|
806
|
+
| <a id="api-fulfilled"></a> `Fulfilled` | `"Fulfilled"` | `'Fulfilled'` |
|
|
807
|
+
| <a id="api-pending"></a> `Pending` | `"Pending"` | `'Pending'` |
|
|
808
|
+
| <a id="api-rejected"></a> `Rejected` | `"Rejected"` | `'Rejected'` |
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
<a id="variablescancellabledelayedrejectmd"></a>
|
|
812
|
+
|
|
813
|
+
### Variable: cancellableDelayedReject()
|
|
814
|
+
|
|
815
|
+
> `const` **cancellableDelayedReject**: \<`T`, `R`\>(`ms`, `reason`) => `object` = `PromiseUtils.cancellableDelayedReject`
|
|
816
|
+
|
|
817
|
+
Creates a cancellable timer that will reject after a specified number of milliseconds.
|
|
818
|
+
|
|
819
|
+
The returned object contains:
|
|
820
|
+
- stop() to cancel the scheduled rejection (if called before the timer fires). Calling
|
|
821
|
+
stop() prevents the promise from being settled by this timer.
|
|
822
|
+
- promise which will reject with the supplied reason (or the value returned by the
|
|
823
|
+
reason function) after ms milliseconds unless stop() is called first.
|
|
824
|
+
|
|
825
|
+
If the reason is a PromiseLike that rejects, its rejection value will be used as the rejection reason.
|
|
648
826
|
|
|
649
827
|
Creates a cancellable timer that will reject after a specified number of milliseconds.
|
|
650
828
|
|
|
@@ -656,36 +834,59 @@ The returned object contains:
|
|
|
656
834
|
|
|
657
835
|
If the `reason` is a PromiseLike that rejects, its rejection value will be used as the rejection reason.
|
|
658
836
|
|
|
659
|
-
|
|
837
|
+
#### Type Parameters
|
|
660
838
|
|
|
661
|
-
|
|
|
662
|
-
|
|
|
839
|
+
| Type Parameter | Default type |
|
|
840
|
+
| ------ | ------ |
|
|
663
841
|
| `T` | `never` |
|
|
664
842
|
| `R` | `any` |
|
|
665
843
|
|
|
666
|
-
|
|
844
|
+
#### Parameters
|
|
667
845
|
|
|
668
|
-
|
|
|
669
|
-
|
|
|
846
|
+
| Parameter | Type | Description |
|
|
847
|
+
| ------ | ------ | ------ |
|
|
670
848
|
| `ms` | `number` | The number of milliseconds after which the scheduled rejection will occur. |
|
|
671
849
|
| `reason` | `R` \| `PromiseLike`\<`R`\> \| () => `R` \| `PromiseLike`\<`R`\> | The reason for the rejection, or a function that supplies the reason. |
|
|
672
850
|
|
|
673
|
-
|
|
851
|
+
#### Returns
|
|
674
852
|
|
|
675
|
-
`
|
|
853
|
+
`object`
|
|
676
854
|
|
|
677
855
|
An object with `stop()` and `promise`.
|
|
678
856
|
|
|
679
857
|
| Name | Type |
|
|
680
|
-
|
|
|
858
|
+
| ------ | ------ |
|
|
681
859
|
| `promise` | `Promise`\<`T`\> |
|
|
682
|
-
| `stop` | () => `void` |
|
|
860
|
+
| `stop()` | () => `void` |
|
|
861
|
+
|
|
862
|
+
#### Param
|
|
683
863
|
|
|
684
|
-
|
|
864
|
+
The number of milliseconds after which the scheduled rejection will occur.
|
|
685
865
|
|
|
686
|
-
|
|
866
|
+
#### Param
|
|
867
|
+
|
|
868
|
+
The reason for the rejection, or a function that supplies the reason.
|
|
869
|
+
|
|
870
|
+
#### Returns
|
|
871
|
+
|
|
872
|
+
An object with stop() and promise.
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
<a id="variablescancellabledelayedresolvemd"></a>
|
|
876
|
+
|
|
877
|
+
### Variable: cancellableDelayedResolve()
|
|
878
|
+
|
|
879
|
+
> `const` **cancellableDelayedResolve**: \<`T`\>(`ms`, `result?`) => `object` = `PromiseUtils.cancellableDelayedResolve`
|
|
880
|
+
|
|
881
|
+
Creates a cancellable timer that will resolve after a specified number of milliseconds.
|
|
882
|
+
|
|
883
|
+
The returned object contains:
|
|
884
|
+
- stop() to cancel the scheduled resolution (if called before the timer fires). Calling
|
|
885
|
+
stop() prevents the promise from being settled by this timer.
|
|
886
|
+
- promise which will resolve with the supplied result (or the value returned by the
|
|
887
|
+
result function) after ms milliseconds unless stop() is called first.
|
|
687
888
|
|
|
688
|
-
|
|
889
|
+
If the result is a PromiseLike, its resolution value will be used as the resolved value.
|
|
689
890
|
|
|
690
891
|
Creates a cancellable timer that will resolve after a specified number of milliseconds.
|
|
691
892
|
|
|
@@ -698,35 +899,59 @@ The returned object contains:
|
|
|
698
899
|
Note: If the `result` is a function that returns a Promise, the returned `promise` will
|
|
699
900
|
resolve with that Promise's resolution (i.e. it behaves like resolving with a PromiseLike).
|
|
700
901
|
|
|
701
|
-
|
|
902
|
+
#### Type Parameters
|
|
702
903
|
|
|
703
|
-
|
|
|
704
|
-
|
|
|
904
|
+
| Type Parameter |
|
|
905
|
+
| ------ |
|
|
705
906
|
| `T` |
|
|
706
907
|
|
|
707
|
-
|
|
908
|
+
#### Parameters
|
|
708
909
|
|
|
709
|
-
|
|
|
710
|
-
|
|
|
910
|
+
| Parameter | Type | Description |
|
|
911
|
+
| ------ | ------ | ------ |
|
|
711
912
|
| `ms` | `number` | The number of milliseconds after which the scheduled resolution will occur. |
|
|
712
913
|
| `result?` | `T` \| `PromiseLike`\<`T`\> \| () => `T` \| `PromiseLike`\<`T`\> | The result to be resolved by the Promise, or a function that supplies the result. |
|
|
713
914
|
|
|
714
|
-
|
|
915
|
+
#### Returns
|
|
715
916
|
|
|
716
|
-
`
|
|
917
|
+
`object`
|
|
717
918
|
|
|
718
919
|
An object with `stop()` and `promise`.
|
|
719
920
|
|
|
720
921
|
| Name | Type |
|
|
721
|
-
|
|
|
922
|
+
| ------ | ------ |
|
|
722
923
|
| `promise` | `Promise`\<`T`\> |
|
|
723
|
-
| `stop` | () => `void` |
|
|
924
|
+
| `stop()` | () => `void` |
|
|
925
|
+
|
|
926
|
+
#### Param
|
|
927
|
+
|
|
928
|
+
The number of milliseconds after which the scheduled resolution will occur.
|
|
929
|
+
|
|
930
|
+
#### Param
|
|
931
|
+
|
|
932
|
+
The result to be resolved by the Promise, or a function that supplies the result.
|
|
933
|
+
|
|
934
|
+
#### Returns
|
|
935
|
+
|
|
936
|
+
An object with stop() and promise.
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
<a id="variablesdelayedrejectmd"></a>
|
|
724
940
|
|
|
725
|
-
|
|
941
|
+
### Variable: delayedReject()
|
|
726
942
|
|
|
727
|
-
|
|
943
|
+
> `const` **delayedReject**: \<`T`, `R`\>(`ms`, `reason`) => `Promise`\<`T`\> = `PromiseUtils.delayedReject`
|
|
728
944
|
|
|
729
|
-
|
|
945
|
+
Creates a Promise that rejects after a specified number of milliseconds.
|
|
946
|
+
|
|
947
|
+
The reason argument may be:
|
|
948
|
+
- a value to reject with,
|
|
949
|
+
- a PromiseLike whose rejection will be adopted by the returned Promise, or
|
|
950
|
+
- a function which is invoked when the timer fires and may return a value or a PromiseLike.
|
|
951
|
+
|
|
952
|
+
If reason is a function, it is called when the timer elapses; if it returns a Promise,
|
|
953
|
+
the returned Promise will reject with that Promise's rejection reason (or reject with the
|
|
954
|
+
returned value if it resolves).
|
|
730
955
|
|
|
731
956
|
Creates a Promise that rejects after a specified number of milliseconds.
|
|
732
957
|
|
|
@@ -739,31 +964,54 @@ If `reason` is a function, it is called when the timer elapses; if it returns a
|
|
|
739
964
|
the returned Promise will reject with that Promise's rejection reason (or reject with the
|
|
740
965
|
returned value if it resolves).
|
|
741
966
|
|
|
742
|
-
|
|
967
|
+
#### Type Parameters
|
|
743
968
|
|
|
744
|
-
|
|
|
745
|
-
|
|
|
969
|
+
| Type Parameter | Default type |
|
|
970
|
+
| ------ | ------ |
|
|
746
971
|
| `T` | `never` |
|
|
747
972
|
| `R` | `any` |
|
|
748
973
|
|
|
749
|
-
|
|
974
|
+
#### Parameters
|
|
750
975
|
|
|
751
|
-
|
|
|
752
|
-
|
|
|
976
|
+
| Parameter | Type | Description |
|
|
977
|
+
| ------ | ------ | ------ |
|
|
753
978
|
| `ms` | `number` | The number of milliseconds after which the created Promise will reject. |
|
|
754
979
|
| `reason` | `R` \| `PromiseLike`\<`R`\> \| () => `R` \| `PromiseLike`\<`R`\> | The reason for the rejection, or a function that supplies the reason. |
|
|
755
980
|
|
|
756
|
-
|
|
981
|
+
#### Returns
|
|
757
982
|
|
|
758
983
|
`Promise`\<`T`\>
|
|
759
984
|
|
|
760
985
|
A Promise that rejects with the specified reason after the specified delay.
|
|
761
986
|
|
|
762
|
-
|
|
987
|
+
#### Param
|
|
988
|
+
|
|
989
|
+
The number of milliseconds after which the created Promise will reject.
|
|
990
|
+
|
|
991
|
+
#### Param
|
|
992
|
+
|
|
993
|
+
The reason for the rejection, or a function that supplies the reason.
|
|
994
|
+
|
|
995
|
+
#### Returns
|
|
996
|
+
|
|
997
|
+
A Promise that rejects with the specified reason after the specified delay.
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
<a id="variablesdelayedresolvemd"></a>
|
|
1001
|
+
|
|
1002
|
+
### Variable: delayedResolve()
|
|
1003
|
+
|
|
1004
|
+
> `const` **delayedResolve**: \<`T`\>(`ms`, `result?`) => `Promise`\<`T`\> = `PromiseUtils.delayedResolve`
|
|
1005
|
+
|
|
1006
|
+
Creates a Promise that resolves after a specified number of milliseconds.
|
|
763
1007
|
|
|
764
|
-
|
|
1008
|
+
The result argument may be:
|
|
1009
|
+
- a value to resolve with,
|
|
1010
|
+
- a PromiseLike whose resolution will be adopted by the returned Promise, or
|
|
1011
|
+
- a function which is invoked when the timer fires and may return a value or a PromiseLike.
|
|
765
1012
|
|
|
766
|
-
|
|
1013
|
+
If result is a function, it is called when the timer elapses; if it returns a Promise,
|
|
1014
|
+
the returned Promise will adopt that Promise's outcome.
|
|
767
1015
|
|
|
768
1016
|
Creates a Promise that resolves after a specified number of milliseconds.
|
|
769
1017
|
|
|
@@ -775,33 +1023,56 @@ The `result` argument may be:
|
|
|
775
1023
|
If `result` is a function, it is called when the timer elapses; if it returns a Promise,
|
|
776
1024
|
the returned Promise will adopt that Promise's outcome.
|
|
777
1025
|
|
|
778
|
-
|
|
1026
|
+
#### Type Parameters
|
|
779
1027
|
|
|
780
|
-
|
|
|
781
|
-
|
|
|
1028
|
+
| Type Parameter |
|
|
1029
|
+
| ------ |
|
|
782
1030
|
| `T` |
|
|
783
1031
|
|
|
784
|
-
|
|
1032
|
+
#### Parameters
|
|
785
1033
|
|
|
786
|
-
|
|
|
787
|
-
|
|
|
1034
|
+
| Parameter | Type | Description |
|
|
1035
|
+
| ------ | ------ | ------ |
|
|
788
1036
|
| `ms` | `number` | The number of milliseconds after which the created Promise will resolve. |
|
|
789
1037
|
| `result?` | `T` \| `PromiseLike`\<`T`\> \| () => `T` \| `PromiseLike`\<`T`\> | The result to be resolved by the Promise, or a function that supplies the result. |
|
|
790
1038
|
|
|
791
|
-
|
|
1039
|
+
#### Returns
|
|
792
1040
|
|
|
793
1041
|
`Promise`\<`T`\>
|
|
794
1042
|
|
|
795
1043
|
A Promise that resolves with the specified result after the specified delay.
|
|
796
1044
|
|
|
797
|
-
|
|
1045
|
+
#### Param
|
|
1046
|
+
|
|
1047
|
+
The number of milliseconds after which the created Promise will resolve.
|
|
1048
|
+
|
|
1049
|
+
#### Param
|
|
1050
|
+
|
|
1051
|
+
The result to be resolved by the Promise, or a function that supplies the result.
|
|
1052
|
+
|
|
1053
|
+
#### Returns
|
|
1054
|
+
|
|
1055
|
+
A Promise that resolves with the specified result after the specified delay.
|
|
1056
|
+
|
|
798
1057
|
|
|
799
|
-
|
|
1058
|
+
<a id="variablesinparallelmd"></a>
|
|
1059
|
+
|
|
1060
|
+
### Variable: inParallel()
|
|
1061
|
+
|
|
1062
|
+
> `const` **inParallel**: \<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`, `options?`) => `Promise`\<(`Result` \| `TError`)[]\> = `PromiseUtils.inParallel`
|
|
1063
|
+
|
|
1064
|
+
Executes multiple jobs/operations in parallel. By default, all operations are executed regardless of any failures.
|
|
1065
|
+
In most cases, using withConcurrency might be more convenient.
|
|
1066
|
+
|
|
1067
|
+
By default, this function does not throw or reject an error when any job/operation fails.
|
|
1068
|
+
Errors from operations are returned alongside results in the returned array.
|
|
1069
|
+
This function only resolves when all jobs/operations are settled (either resolved or rejected).
|
|
800
1070
|
|
|
801
|
-
|
|
1071
|
+
If options.abortOnError is set to true, this function throws (or rejects with) an error immediately when any job/operation fails.
|
|
1072
|
+
In this mode, some jobs/operations may not be executed if one fails.
|
|
802
1073
|
|
|
803
1074
|
Executes multiple jobs/operations in parallel. By default, all operations are executed regardless of any failures.
|
|
804
|
-
In most cases, using [withConcurrency](#withconcurrency) might be more convenient.
|
|
1075
|
+
In most cases, using [PromiseUtils.withConcurrency](#withconcurrency) might be more convenient.
|
|
805
1076
|
|
|
806
1077
|
By default, this function does not throw or reject an error when any job/operation fails.
|
|
807
1078
|
Errors from operations are returned alongside results in the returned array.
|
|
@@ -810,25 +1081,25 @@ This function only resolves when all jobs/operations are settled (either resolve
|
|
|
810
1081
|
If `options.abortOnError` is set to true, this function throws (or rejects with) an error immediately when any job/operation fails.
|
|
811
1082
|
In this mode, some jobs/operations may not be executed if one fails.
|
|
812
1083
|
|
|
813
|
-
|
|
1084
|
+
#### Type Parameters
|
|
814
1085
|
|
|
815
|
-
|
|
|
816
|
-
|
|
|
817
|
-
| `Data` |
|
|
818
|
-
| `Result` |
|
|
1086
|
+
| Type Parameter | Default type | Description |
|
|
1087
|
+
| ------ | ------ | ------ |
|
|
1088
|
+
| `Data` | - | The type of the job data, typically an Array. |
|
|
1089
|
+
| `Result` | - | The type of the return value from the operation function. |
|
|
819
1090
|
| `TError` | `Result` | The type for the error that could be thrown from the operation function, defaults to `Result`. |
|
|
820
1091
|
|
|
821
|
-
|
|
1092
|
+
#### Parameters
|
|
822
1093
|
|
|
823
|
-
|
|
|
824
|
-
|
|
|
1094
|
+
| Parameter | Type | Description |
|
|
1095
|
+
| ------ | ------ | ------ |
|
|
825
1096
|
| `parallelism` | `number` | The number of jobs/operations to run concurrently. |
|
|
826
1097
|
| `jobs` | `Iterable`\<`Data`\> | The job data to be processed. This function can safely handle an infinite or unknown number of elements. |
|
|
827
|
-
| `operation` | (`job
|
|
828
|
-
| `options?` | `
|
|
829
|
-
| `options.abortOnError
|
|
1098
|
+
| `operation` | (`job`, `index`) => `Promise`\<`Result`\> | The function that processes job data asynchronously. |
|
|
1099
|
+
| `options?` | \{ `abortOnError`: `boolean`; \} | Options to control the function's behavior. |
|
|
1100
|
+
| `options.abortOnError?` | `boolean` | If true, the function aborts and throws an error on the first failed operation. |
|
|
830
1101
|
|
|
831
|
-
|
|
1102
|
+
#### Returns
|
|
832
1103
|
|
|
833
1104
|
`Promise`\<(`Result` \| `TError`)[]\>
|
|
834
1105
|
|
|
@@ -836,7 +1107,7 @@ A promise that resolves to an array containing the results of the operations.
|
|
|
836
1107
|
Each element is either a fulfilled result or a rejected error/reason.
|
|
837
1108
|
The results or errors in the returned array are in the same order as the corresponding elements in the jobs array.
|
|
838
1109
|
|
|
839
|
-
|
|
1110
|
+
#### Example
|
|
840
1111
|
|
|
841
1112
|
```ts
|
|
842
1113
|
// Capture errors in the returned array
|
|
@@ -854,61 +1125,103 @@ try {
|
|
|
854
1125
|
}
|
|
855
1126
|
```
|
|
856
1127
|
|
|
857
|
-
|
|
1128
|
+
#### Param
|
|
1129
|
+
|
|
1130
|
+
The number of jobs/operations to run concurrently.
|
|
1131
|
+
|
|
1132
|
+
#### Param
|
|
1133
|
+
|
|
1134
|
+
The job data to be processed. This function can safely handle an infinite or unknown number of elements.
|
|
1135
|
+
|
|
1136
|
+
#### Param
|
|
1137
|
+
|
|
1138
|
+
The function that processes job data asynchronously.
|
|
1139
|
+
|
|
1140
|
+
#### Param
|
|
1141
|
+
|
|
1142
|
+
Options to control the function's behavior.
|
|
1143
|
+
|
|
1144
|
+
#### Param
|
|
1145
|
+
|
|
1146
|
+
If true, the function aborts and throws an error on the first failed operation.
|
|
1147
|
+
|
|
1148
|
+
#### Returns
|
|
1149
|
+
|
|
1150
|
+
A promise that resolves to an array containing the results of the operations.
|
|
1151
|
+
Each element is either a fulfilled result or a rejected error/reason.
|
|
1152
|
+
The results or errors in the returned array are in the same order as the corresponding elements in the jobs array.
|
|
1153
|
+
|
|
858
1154
|
|
|
859
|
-
|
|
1155
|
+
<a id="variablespromisestatemd"></a>
|
|
860
1156
|
|
|
861
|
-
|
|
1157
|
+
### Variable: promiseState()
|
|
1158
|
+
|
|
1159
|
+
> `const` **promiseState**: (`p`) => `Promise`\<`"Pending"` \| `"Fulfilled"` \| `"Rejected"`\> = `PromiseUtils.promiseState`
|
|
862
1160
|
|
|
863
1161
|
Retrieves the state of the specified Promise.
|
|
864
1162
|
Note: The returned value is a Promise that resolves immediately.
|
|
865
1163
|
|
|
866
|
-
|
|
1164
|
+
Retrieves the state of the specified Promise.
|
|
1165
|
+
Note: The returned value is a Promise that resolves immediately.
|
|
867
1166
|
|
|
868
|
-
|
|
869
|
-
|
|
1167
|
+
#### Parameters
|
|
1168
|
+
|
|
1169
|
+
| Parameter | Type | Description |
|
|
1170
|
+
| ------ | ------ | ------ |
|
|
870
1171
|
| `p` | `Promise`\<`any`\> | The Promise whose state is to be determined. |
|
|
871
1172
|
|
|
872
|
-
|
|
1173
|
+
#### Returns
|
|
1174
|
+
|
|
1175
|
+
`Promise`\<`"Pending"` \| `"Fulfilled"` \| `"Rejected"`\>
|
|
1176
|
+
|
|
1177
|
+
A Promise that resolves immediately with the state of the input Promise.
|
|
1178
|
+
|
|
1179
|
+
#### Param
|
|
873
1180
|
|
|
874
|
-
|
|
1181
|
+
The Promise whose state is to be determined.
|
|
1182
|
+
|
|
1183
|
+
#### Returns
|
|
875
1184
|
|
|
876
1185
|
A Promise that resolves immediately with the state of the input Promise.
|
|
877
1186
|
|
|
878
|
-
___
|
|
879
1187
|
|
|
880
|
-
|
|
1188
|
+
<a id="variablesrepeatmd"></a>
|
|
1189
|
+
|
|
1190
|
+
### Variable: repeat()
|
|
881
1191
|
|
|
882
|
-
|
|
1192
|
+
> `const` **repeat**: \<`Result`, `Param`, `Collection`\>(`operation`, `nextParameter`, `collect`, `initialCollection`, `initialParameter`) => `Promise`\<`Collection`\> = `PromiseUtils.repeat`
|
|
883
1193
|
|
|
884
1194
|
Executes an operation repeatedly and collects all the results.
|
|
885
1195
|
This function is very useful for many scenarios, such like client-side pagination.
|
|
886
1196
|
|
|
887
|
-
|
|
1197
|
+
Executes an operation repeatedly and collects all the results.
|
|
1198
|
+
This function is very useful for many scenarios, such like client-side pagination.
|
|
888
1199
|
|
|
889
|
-
|
|
890
|
-
|
|
1200
|
+
#### Type Parameters
|
|
1201
|
+
|
|
1202
|
+
| Type Parameter | Description |
|
|
1203
|
+
| ------ | ------ |
|
|
891
1204
|
| `Result` | The type of the operation result. |
|
|
892
1205
|
| `Param` | The type of the input to the operation, typically a paging parameter. |
|
|
893
1206
|
| `Collection` | The type of the collection returned by this function. |
|
|
894
1207
|
|
|
895
|
-
|
|
1208
|
+
#### Parameters
|
|
896
1209
|
|
|
897
|
-
|
|
|
898
|
-
|
|
|
899
|
-
| `operation` | (`parameter
|
|
900
|
-
| `nextParameter` | (`response
|
|
901
|
-
| `collect` | (`collection
|
|
1210
|
+
| Parameter | Type | Description |
|
|
1211
|
+
| ------ | ------ | ------ |
|
|
1212
|
+
| `operation` | (`parameter`) => `Promise`\<`Result`\> | A function that takes a parameter as input and returns a result. Typically, the parameter has optional fields to control paging. |
|
|
1213
|
+
| `nextParameter` | (`response`) => `Partial`\<`Param`\> \| `Promise`\<`Partial`\<`Param`\>\> \| `null` | A function for calculating the next parameter from the operation result. Normally, this parameter controls paging. This function should return null when no further invocation of the operation function is desired. If further invocation is desired, the return value of this function can be a Promise or a non-Promise value. |
|
|
1214
|
+
| `collect` | (`collection`, `result`) => `Collection` | A function for merging the operation result into the collection. |
|
|
902
1215
|
| `initialCollection` | `Collection` | The initial collection, which will be the first argument passed to the first invocation of the collect function. |
|
|
903
1216
|
| `initialParameter` | `Partial`\<`Param`\> | The parameter for the first operation. |
|
|
904
1217
|
|
|
905
|
-
|
|
1218
|
+
#### Returns
|
|
906
1219
|
|
|
907
1220
|
`Promise`\<`Collection`\>
|
|
908
1221
|
|
|
909
1222
|
A promise that resolves to a collection of all the results returned by the operation function.
|
|
910
1223
|
|
|
911
|
-
|
|
1224
|
+
#### Example
|
|
912
1225
|
|
|
913
1226
|
```ts
|
|
914
1227
|
const domainNameObjects = await PromiseUtils.repeat(
|
|
@@ -919,11 +1232,62 @@ const domainNameObjects = await PromiseUtils.repeat(
|
|
|
919
1232
|
);
|
|
920
1233
|
```
|
|
921
1234
|
|
|
922
|
-
|
|
1235
|
+
#### Param
|
|
1236
|
+
|
|
1237
|
+
A function that takes a parameter as input and returns a result. Typically, the parameter has optional fields to control paging.
|
|
1238
|
+
|
|
1239
|
+
#### Param
|
|
1240
|
+
|
|
1241
|
+
A function for calculating the next parameter from the operation result. Normally, this parameter controls paging. This function should return null when no further invocation of the operation function is desired. If further invocation is desired, the return value of this function can be a Promise or a non-Promise value.
|
|
1242
|
+
|
|
1243
|
+
#### Param
|
|
1244
|
+
|
|
1245
|
+
A function for merging the operation result into the collection.
|
|
1246
|
+
|
|
1247
|
+
#### Param
|
|
1248
|
+
|
|
1249
|
+
The initial collection, which will be the first argument passed to the first invocation of the collect function.
|
|
1250
|
+
|
|
1251
|
+
#### Param
|
|
1252
|
+
|
|
1253
|
+
The parameter for the first operation.
|
|
1254
|
+
|
|
1255
|
+
#### Returns
|
|
1256
|
+
|
|
1257
|
+
A promise that resolves to a collection of all the results returned by the operation function.
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
<a id="variablesrunperiodicallymd"></a>
|
|
1261
|
+
|
|
1262
|
+
### Variable: runPeriodically()
|
|
1263
|
+
|
|
1264
|
+
> `const` **runPeriodically**: \<`T`\>(`operation`, `interval`, `options?`) => `object` = `PromiseUtils.runPeriodically`
|
|
1265
|
+
|
|
1266
|
+
Runs an operation periodically with configurable intervals and stopping conditions.
|
|
1267
|
+
|
|
1268
|
+
- `interval` may be a single number (ms), an array of numbers, or a function
|
|
1269
|
+
that receives the iteration number (starting at 1) and returns the next
|
|
1270
|
+
interval in milliseconds or `undefined` to stop.
|
|
1271
|
+
- If the interval array runs out of elements or the function returns `undefined`
|
|
1272
|
+
(or a negative value), no further invocations will be scheduled.
|
|
1273
|
+
|
|
1274
|
+
Options:
|
|
1275
|
+
- `maxExecutions` stop after N runs (inclusive).
|
|
1276
|
+
- `maxDurationMs` stop after elapsed ms since the first scheduled start.
|
|
1277
|
+
- `schedule` controls how the interval is measured:
|
|
1278
|
+
- `'delayAfterEnd'`: wait the interval after the previous operation completes
|
|
1279
|
+
before scheduling the next one (equivalent to a fixed delay between ends).
|
|
1280
|
+
- `'delayBetweenStarts'`: keep start times on a regular schedule (interval measured
|
|
1281
|
+
between the starts of successive operations).
|
|
1282
|
+
The default schedule is `'delayBetweenStarts'`.
|
|
923
1283
|
|
|
924
|
-
|
|
1284
|
+
Returns an object with `stop()` to cancel further executions and `done` which
|
|
1285
|
+
resolves when the periodic runner stops. If the provided `operation` throws or
|
|
1286
|
+
rejects, the `done` promise will reject with that error so callers can handle it.
|
|
925
1287
|
|
|
926
|
-
|
|
1288
|
+
Note: The first invocation of `operation` is scheduled after the first interval
|
|
1289
|
+
elapses (i.e. this function does NOT call `operation` immediately). If you need
|
|
1290
|
+
an immediate run, invoke `operation(1)` yourself before calling `runPeriodically`.
|
|
927
1291
|
|
|
928
1292
|
Runs an operation periodically with configurable intervals and stopping conditions.
|
|
929
1293
|
|
|
@@ -951,67 +1315,123 @@ Note: The first invocation of `operation` is scheduled after the first interval
|
|
|
951
1315
|
elapses (i.e. this function does NOT call `operation` immediately). If you need
|
|
952
1316
|
an immediate run, invoke `operation(1)` yourself before calling `runPeriodically`.
|
|
953
1317
|
|
|
954
|
-
|
|
1318
|
+
#### Type Parameters
|
|
955
1319
|
|
|
956
|
-
|
|
|
957
|
-
|
|
|
1320
|
+
| Type Parameter | Description |
|
|
1321
|
+
| ------ | ------ |
|
|
958
1322
|
| `T` | The operation return type (ignored by the runner; used for typing). |
|
|
959
1323
|
|
|
960
|
-
|
|
1324
|
+
#### Parameters
|
|
961
1325
|
|
|
962
|
-
|
|
|
963
|
-
|
|
|
964
|
-
| `operation` | (`iteration
|
|
965
|
-
| `interval` | `number` \| `number`[] \| (`iteration
|
|
966
|
-
| `options?` | `
|
|
967
|
-
| `options.maxDurationMs?` | `number` |
|
|
968
|
-
| `options.maxExecutions?` | `number` |
|
|
969
|
-
| `options.schedule?` |
|
|
1326
|
+
| Parameter | Type | Description |
|
|
1327
|
+
| ------ | ------ | ------ |
|
|
1328
|
+
| `operation` | (`iteration`) => `T` \| `Promise`\<`T`\> | Function to run each iteration. Receives the iteration index (1-based). |
|
|
1329
|
+
| `interval` | `number` \| `number`[] \| (`iteration`) => `number` \| `undefined` | Number | number[] | ((iteration: number) => number|undefined) defining waits. |
|
|
1330
|
+
| `options?` | \{ `maxDurationMs?`: `number`; `maxExecutions?`: `number`; `schedule?`: `"delayAfterEnd"` \| `"delayBetweenStarts"`; \} | Optional configuration. |
|
|
1331
|
+
| `options.maxDurationMs?` | `number` | Stop after N milliseconds. |
|
|
1332
|
+
| `options.maxExecutions?` | `number` | Stop after N executions. |
|
|
1333
|
+
| `options.schedule?` | `"delayAfterEnd"` \| `"delayBetweenStarts"` | How to measure intervals: `'delayAfterEnd'` or `'delayBetweenStarts'`. |
|
|
970
1334
|
|
|
971
|
-
|
|
1335
|
+
#### Returns
|
|
972
1336
|
|
|
973
|
-
`
|
|
1337
|
+
`object`
|
|
974
1338
|
|
|
975
1339
|
An object containing `stop()` to cancel further executions and `done` Promise
|
|
976
1340
|
which resolves when the periodic runner stops (or rejects if the operation errors).
|
|
977
1341
|
|
|
978
1342
|
| Name | Type |
|
|
979
|
-
|
|
|
1343
|
+
| ------ | ------ |
|
|
980
1344
|
| `done` | `Promise`\<`void`\> |
|
|
981
|
-
| `stop` | () => `void` |
|
|
1345
|
+
| `stop()` | () => `void` |
|
|
1346
|
+
|
|
1347
|
+
#### Template
|
|
1348
|
+
|
|
1349
|
+
The operation return type (ignored by the runner; used for typing).
|
|
1350
|
+
|
|
1351
|
+
#### Param
|
|
1352
|
+
|
|
1353
|
+
Function to run each iteration. Receives the iteration index (1-based).
|
|
1354
|
+
|
|
1355
|
+
#### Param
|
|
1356
|
+
|
|
1357
|
+
Number | number[] | ((iteration: number) => number|undefined) defining waits.
|
|
1358
|
+
|
|
1359
|
+
#### Param
|
|
1360
|
+
|
|
1361
|
+
Optional configuration.
|
|
1362
|
+
|
|
1363
|
+
#### Param
|
|
1364
|
+
|
|
1365
|
+
Stop after N executions.
|
|
1366
|
+
|
|
1367
|
+
#### Param
|
|
1368
|
+
|
|
1369
|
+
Stop after N milliseconds.
|
|
1370
|
+
|
|
1371
|
+
#### Param
|
|
1372
|
+
|
|
1373
|
+
How to measure intervals: `'delayAfterEnd'` or `'delayBetweenStarts'`.
|
|
1374
|
+
|
|
1375
|
+
#### Returns
|
|
1376
|
+
|
|
1377
|
+
An object containing `stop()` to cancel further executions and `done` Promise
|
|
1378
|
+
which resolves when the periodic runner stops (or rejects if the operation errors).
|
|
1379
|
+
|
|
982
1380
|
|
|
983
|
-
|
|
1381
|
+
<a id="variablessynchronisedmd"></a>
|
|
984
1382
|
|
|
985
|
-
|
|
1383
|
+
### Variable: synchronised()
|
|
986
1384
|
|
|
987
|
-
|
|
1385
|
+
> `const` **synchronised**: \<`T`\>(`lock`, `operation`) => `Promise`\<`T`\> = `PromiseUtils.synchronised`
|
|
1386
|
+
|
|
1387
|
+
This is just another spelling of synchronized.
|
|
988
1388
|
|
|
989
|
-
This is just another spelling of [synchronized](#synchronized).
|
|
1389
|
+
This is just another spelling of [PromiseUtils.synchronized](#synchronized).
|
|
990
1390
|
|
|
991
|
-
|
|
1391
|
+
#### Type Parameters
|
|
992
1392
|
|
|
993
|
-
|
|
|
994
|
-
|
|
|
1393
|
+
| Type Parameter |
|
|
1394
|
+
| ------ |
|
|
995
1395
|
| `T` |
|
|
996
1396
|
|
|
997
|
-
|
|
1397
|
+
#### Parameters
|
|
998
1398
|
|
|
999
|
-
|
|
|
1000
|
-
|
|
|
1399
|
+
| Parameter | Type | Description |
|
|
1400
|
+
| ------ | ------ | ------ |
|
|
1001
1401
|
| `lock` | `any` | The object (such as a string, a number, or `this` in a class) used to identify the lock. |
|
|
1002
|
-
| `operation` | (`previousState
|
|
1402
|
+
| `operation` | (`previousState`, `previousSettledState`, `previousResult`) => `Promise`\<`T`\> | The function that performs the computation and returns a Promise. |
|
|
1003
1403
|
|
|
1004
|
-
|
|
1404
|
+
#### Returns
|
|
1005
1405
|
|
|
1006
1406
|
`Promise`\<`T`\>
|
|
1007
1407
|
|
|
1008
1408
|
The result of the operation function.
|
|
1009
1409
|
|
|
1010
|
-
|
|
1410
|
+
#### Param
|
|
1411
|
+
|
|
1412
|
+
The object (such as a string, a number, or this in a class) used to identify the lock.
|
|
1413
|
+
|
|
1414
|
+
#### Param
|
|
1415
|
+
|
|
1416
|
+
The function that performs the computation and returns a Promise.
|
|
1417
|
+
|
|
1418
|
+
#### Returns
|
|
1419
|
+
|
|
1420
|
+
The result of the operation function.
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
<a id="variablessynchronizedmd"></a>
|
|
1011
1424
|
|
|
1012
|
-
|
|
1425
|
+
### Variable: synchronized()
|
|
1013
1426
|
|
|
1014
|
-
|
|
1427
|
+
> `const` **synchronized**: \<`T`\>(`lock`, `operation`) => `Promise`\<`T`\> = `PromiseUtils.synchronized`
|
|
1428
|
+
|
|
1429
|
+
Provides mutual exclusion similar to synchronized in Java.
|
|
1430
|
+
Ensures no concurrent execution of any operation function associated with the same lock.
|
|
1431
|
+
The operation function has access to the state (when synchronized is called),
|
|
1432
|
+
settledState (when the operation function is called),
|
|
1433
|
+
and result (either the fulfilled result or the rejected reason) of the previous operation.
|
|
1434
|
+
If there is no previous invocation, state, settledState, and result will all be undefined.
|
|
1015
1435
|
|
|
1016
1436
|
Provides mutual exclusion similar to `synchronized` in Java.
|
|
1017
1437
|
Ensures no concurrent execution of any operation function associated with the same lock.
|
|
@@ -1020,30 +1440,49 @@ settledState (when the operation function is called),
|
|
|
1020
1440
|
and result (either the fulfilled result or the rejected reason) of the previous operation.
|
|
1021
1441
|
If there is no previous invocation, state, settledState, and result will all be undefined.
|
|
1022
1442
|
|
|
1023
|
-
|
|
1443
|
+
#### Type Parameters
|
|
1024
1444
|
|
|
1025
|
-
|
|
|
1026
|
-
|
|
|
1445
|
+
| Type Parameter |
|
|
1446
|
+
| ------ |
|
|
1027
1447
|
| `T` |
|
|
1028
1448
|
|
|
1029
|
-
|
|
1449
|
+
#### Parameters
|
|
1030
1450
|
|
|
1031
|
-
|
|
|
1032
|
-
|
|
|
1451
|
+
| Parameter | Type | Description |
|
|
1452
|
+
| ------ | ------ | ------ |
|
|
1033
1453
|
| `lock` | `any` | The object (such as a string, a number, or `this` in a class) used to identify the lock. |
|
|
1034
|
-
| `operation` | (`previousState
|
|
1454
|
+
| `operation` | (`previousState`, `previousSettledState`, `previousResult`) => `Promise`\<`T`\> | The function that performs the computation and returns a Promise. |
|
|
1035
1455
|
|
|
1036
|
-
|
|
1456
|
+
#### Returns
|
|
1037
1457
|
|
|
1038
1458
|
`Promise`\<`T`\>
|
|
1039
1459
|
|
|
1040
1460
|
The result of the operation function.
|
|
1041
1461
|
|
|
1042
|
-
|
|
1462
|
+
#### Param
|
|
1463
|
+
|
|
1464
|
+
The object (such as a string, a number, or this in a class) used to identify the lock.
|
|
1465
|
+
|
|
1466
|
+
#### Param
|
|
1467
|
+
|
|
1468
|
+
The function that performs the computation and returns a Promise.
|
|
1469
|
+
|
|
1470
|
+
#### Returns
|
|
1471
|
+
|
|
1472
|
+
The result of the operation function.
|
|
1473
|
+
|
|
1474
|
+
|
|
1475
|
+
<a id="variablestimeoutrejectmd"></a>
|
|
1476
|
+
|
|
1477
|
+
### Variable: timeoutReject()
|
|
1043
1478
|
|
|
1044
|
-
|
|
1479
|
+
> `const` **timeoutReject**: \<`T`, `R`\>(`operation`, `ms`, `rejectReason`) => `Promise`\<`T`\> = `PromiseUtils.timeoutReject`
|
|
1045
1480
|
|
|
1046
|
-
|
|
1481
|
+
Applies a timeout to a Promise or a function that returns a Promise.
|
|
1482
|
+
If the timeout occurs, the returned Promise rejects with the specified reason.
|
|
1483
|
+
If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise.
|
|
1484
|
+
If the rejectReason parameter is a function and the timeout does not occur, the function will not be called.
|
|
1485
|
+
Note: The rejection of the operation parameter is not handled by this function. You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
1047
1486
|
|
|
1048
1487
|
Applies a timeout to a Promise or a function that returns a Promise.
|
|
1049
1488
|
If the timeout occurs, the returned Promise rejects with the specified reason.
|
|
@@ -1051,32 +1490,56 @@ If the timeout does not occur, the returned Promise resolves or rejects based on
|
|
|
1051
1490
|
If the `rejectReason` parameter is a function and the timeout does not occur, the function will not be called.
|
|
1052
1491
|
Note: The rejection of the `operation` parameter is not handled by this function. You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
1053
1492
|
|
|
1054
|
-
|
|
1493
|
+
#### Type Parameters
|
|
1055
1494
|
|
|
1056
|
-
|
|
|
1057
|
-
|
|
|
1495
|
+
| Type Parameter | Default type |
|
|
1496
|
+
| ------ | ------ |
|
|
1058
1497
|
| `T` | `never` |
|
|
1059
1498
|
| `R` | `any` |
|
|
1060
1499
|
|
|
1061
|
-
|
|
1500
|
+
#### Parameters
|
|
1062
1501
|
|
|
1063
|
-
|
|
|
1064
|
-
|
|
|
1502
|
+
| Parameter | Type | Description |
|
|
1503
|
+
| ------ | ------ | ------ |
|
|
1065
1504
|
| `operation` | `Promise`\<`T`\> \| () => `Promise`\<`T`\> | The original Promise or a function that returns a Promise to which the timeout will be applied. |
|
|
1066
1505
|
| `ms` | `number` | The number of milliseconds for the timeout. |
|
|
1067
1506
|
| `rejectReason` | `R` \| `PromiseLike`\<`R`\> \| () => `R` \| `PromiseLike`\<`R`\> | The reason to reject with if the timeout occurs, or a function that supplies the reason. |
|
|
1068
1507
|
|
|
1069
|
-
|
|
1508
|
+
#### Returns
|
|
1070
1509
|
|
|
1071
1510
|
`Promise`\<`T`\>
|
|
1072
1511
|
|
|
1073
1512
|
A new Promise that rejects with the specified reason if the timeout occurs.
|
|
1074
1513
|
|
|
1075
|
-
|
|
1514
|
+
#### Param
|
|
1515
|
+
|
|
1516
|
+
The original Promise or a function that returns a Promise to which the timeout will be applied.
|
|
1517
|
+
|
|
1518
|
+
#### Param
|
|
1519
|
+
|
|
1520
|
+
The number of milliseconds for the timeout.
|
|
1521
|
+
|
|
1522
|
+
#### Param
|
|
1523
|
+
|
|
1524
|
+
The reason to reject with if the timeout occurs, or a function that supplies the reason.
|
|
1525
|
+
|
|
1526
|
+
#### Returns
|
|
1527
|
+
|
|
1528
|
+
A new Promise that rejects with the specified reason if the timeout occurs.
|
|
1529
|
+
|
|
1530
|
+
|
|
1531
|
+
<a id="variablestimeoutresolvemd"></a>
|
|
1076
1532
|
|
|
1077
|
-
|
|
1533
|
+
### Variable: timeoutResolve()
|
|
1078
1534
|
|
|
1079
|
-
|
|
1535
|
+
> `const` **timeoutResolve**: \<`T`\>(`operation`, `ms`, `result?`) => `Promise`\<`T`\> = `PromiseUtils.timeoutResolve`
|
|
1536
|
+
|
|
1537
|
+
Applies a timeout to a Promise or a function that returns a Promise.
|
|
1538
|
+
If the timeout occurs, the returned Promise resolves to the specified result.
|
|
1539
|
+
If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise.
|
|
1540
|
+
If the result parameter is a function and the timeout does not occur, the function will not be called.
|
|
1541
|
+
Note: The rejection of the operation parameter is not handled by this function.
|
|
1542
|
+
You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
1080
1543
|
|
|
1081
1544
|
Applies a timeout to a Promise or a function that returns a Promise.
|
|
1082
1545
|
If the timeout occurs, the returned Promise resolves to the specified result.
|
|
@@ -1085,61 +1548,80 @@ If the `result` parameter is a function and the timeout does not occur, the func
|
|
|
1085
1548
|
Note: The rejection of the `operation` parameter is not handled by this function.
|
|
1086
1549
|
You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
1087
1550
|
|
|
1088
|
-
|
|
1551
|
+
#### Type Parameters
|
|
1089
1552
|
|
|
1090
|
-
|
|
|
1091
|
-
|
|
|
1553
|
+
| Type Parameter |
|
|
1554
|
+
| ------ |
|
|
1092
1555
|
| `T` |
|
|
1093
1556
|
|
|
1094
|
-
|
|
1557
|
+
#### Parameters
|
|
1095
1558
|
|
|
1096
|
-
|
|
|
1097
|
-
|
|
|
1559
|
+
| Parameter | Type | Description |
|
|
1560
|
+
| ------ | ------ | ------ |
|
|
1098
1561
|
| `operation` | `Promise`\<`T`\> \| () => `Promise`\<`T`\> | The original Promise or a function that returns a Promise to which the timeout will be applied. |
|
|
1099
1562
|
| `ms` | `number` | The number of milliseconds for the timeout. |
|
|
1100
1563
|
| `result?` | `T` \| `PromiseLike`\<`T`\> \| () => `T` \| `PromiseLike`\<`T`\> | The result to resolve with if the timeout occurs, or a function that supplies the result. |
|
|
1101
1564
|
|
|
1102
|
-
|
|
1565
|
+
#### Returns
|
|
1103
1566
|
|
|
1104
1567
|
`Promise`\<`T`\>
|
|
1105
1568
|
|
|
1106
1569
|
A new Promise that resolves to the specified result if the timeout occurs.
|
|
1107
1570
|
|
|
1108
|
-
|
|
1571
|
+
#### Param
|
|
1572
|
+
|
|
1573
|
+
The original Promise or a function that returns a Promise to which the timeout will be applied.
|
|
1574
|
+
|
|
1575
|
+
#### Param
|
|
1576
|
+
|
|
1577
|
+
The number of milliseconds for the timeout.
|
|
1578
|
+
|
|
1579
|
+
#### Param
|
|
1580
|
+
|
|
1581
|
+
The result to resolve with if the timeout occurs, or a function that supplies the result.
|
|
1582
|
+
|
|
1583
|
+
#### Returns
|
|
1584
|
+
|
|
1585
|
+
A new Promise that resolves to the specified result if the timeout occurs.
|
|
1586
|
+
|
|
1109
1587
|
|
|
1110
|
-
|
|
1588
|
+
<a id="variableswithconcurrencymd"></a>
|
|
1111
1589
|
|
|
1112
|
-
|
|
1590
|
+
### Variable: withConcurrency()
|
|
1591
|
+
|
|
1592
|
+
> `const` **withConcurrency**: \<`Data`, `Result`\>(`concurrency`, `jobs`, `operation`) => `Promise`\<`Result`[]\> = `PromiseUtils.withConcurrency`
|
|
1593
|
+
|
|
1594
|
+
Executes multiple jobs/operations with a specified level of concurrency.
|
|
1113
1595
|
|
|
1114
1596
|
Executes multiple jobs/operations with a specified level of concurrency.
|
|
1115
1597
|
|
|
1116
1598
|
Unlike `inParallel(...)`, this function may throw or reject an error when a job/operation fails.
|
|
1117
1599
|
When an error is re-thrown, remaining operations will not be executed.
|
|
1118
|
-
If you want all the operations to always be executed, use [inParallel](#inparallel) instead.
|
|
1600
|
+
If you want all the operations to always be executed, use [PromiseUtils.inParallel](#inparallel) instead.
|
|
1119
1601
|
|
|
1120
|
-
|
|
1602
|
+
#### Type Parameters
|
|
1121
1603
|
|
|
1122
|
-
|
|
|
1123
|
-
|
|
|
1604
|
+
| Type Parameter | Description |
|
|
1605
|
+
| ------ | ------ |
|
|
1124
1606
|
| `Data` | The type of the job data, typically an Array. |
|
|
1125
1607
|
| `Result` | The type of the return value from the operation function. |
|
|
1126
1608
|
|
|
1127
|
-
|
|
1609
|
+
#### Parameters
|
|
1128
1610
|
|
|
1129
|
-
|
|
|
1130
|
-
|
|
|
1611
|
+
| Parameter | Type | Description |
|
|
1612
|
+
| ------ | ------ | ------ |
|
|
1131
1613
|
| `concurrency` | `number` | The number of jobs/operations to run concurrently. |
|
|
1132
1614
|
| `jobs` | `Iterable`\<`Data`\> | The job data to be processed. This function can handle an infinite or unknown number of elements safely. |
|
|
1133
|
-
| `operation` | (`job
|
|
1615
|
+
| `operation` | (`job`, `index`) => `Promise`\<`Result`\> | The function that processes job data asynchronously. |
|
|
1134
1616
|
|
|
1135
|
-
|
|
1617
|
+
#### Returns
|
|
1136
1618
|
|
|
1137
1619
|
`Promise`\<`Result`[]\>
|
|
1138
1620
|
|
|
1139
1621
|
A promise that resolves to an array containing the results from the operation function.
|
|
1140
1622
|
The results in the returned array are in the same order as the corresponding elements in the jobs array.
|
|
1141
1623
|
|
|
1142
|
-
|
|
1624
|
+
#### Example
|
|
1143
1625
|
|
|
1144
1626
|
```ts
|
|
1145
1627
|
// At any time, there would be no more than 5 concurrency API calls. Error would be re-thrown immediately when it occurs.
|
|
@@ -1149,36 +1631,56 @@ const attributes = await PromiseUtils.withConcurrency(5, topicArns, async (topic
|
|
|
1149
1631
|
});
|
|
1150
1632
|
```
|
|
1151
1633
|
|
|
1152
|
-
|
|
1634
|
+
#### Param
|
|
1635
|
+
|
|
1636
|
+
The number of jobs/operations to run concurrently.
|
|
1637
|
+
|
|
1638
|
+
#### Param
|
|
1639
|
+
|
|
1640
|
+
The job data to be processed. This function can handle an infinite or unknown number of elements safely.
|
|
1641
|
+
|
|
1642
|
+
#### Param
|
|
1643
|
+
|
|
1644
|
+
The function that processes job data asynchronously.
|
|
1645
|
+
|
|
1646
|
+
#### Returns
|
|
1647
|
+
|
|
1648
|
+
A promise that resolves to an array containing the results from the operation function.
|
|
1649
|
+
The results in the returned array are in the same order as the corresponding elements in the jobs array.
|
|
1650
|
+
|
|
1651
|
+
|
|
1652
|
+
<a id="variableswithretrymd"></a>
|
|
1153
1653
|
|
|
1154
|
-
|
|
1654
|
+
### Variable: withRetry()
|
|
1155
1655
|
|
|
1156
|
-
|
|
1656
|
+
> `const` **withRetry**: \<`Result`, `TError`\>(`operation`, `backoff`, `shouldRetry`) => `Promise`\<`Result`\> = `PromiseUtils.withRetry`
|
|
1157
1657
|
|
|
1158
1658
|
Repeatedly performs an operation until a specified criteria is met.
|
|
1159
1659
|
|
|
1160
|
-
|
|
1660
|
+
Repeatedly performs an operation until a specified criteria is met.
|
|
1661
|
+
|
|
1662
|
+
#### Type Parameters
|
|
1161
1663
|
|
|
1162
|
-
|
|
|
1163
|
-
|
|
|
1164
|
-
| `Result` |
|
|
1664
|
+
| Type Parameter | Default type | Description |
|
|
1665
|
+
| ------ | ------ | ------ |
|
|
1666
|
+
| `Result` | - | Type of the operation result. |
|
|
1165
1667
|
| `TError` | `any` | Type of the possible error that could be generated by the operation. |
|
|
1166
1668
|
|
|
1167
|
-
|
|
1669
|
+
#### Parameters
|
|
1168
1670
|
|
|
1169
|
-
|
|
|
1170
|
-
|
|
|
1171
|
-
| `operation` | (`attempt
|
|
1172
|
-
| `backoff` | `number`[] \| (`attempt
|
|
1173
|
-
| `shouldRetry` | (`previousError
|
|
1671
|
+
| Parameter | Type | Description |
|
|
1672
|
+
| ------ | ------ | ------ |
|
|
1673
|
+
| `operation` | (`attempt`, `previousResult`, `previousError`) => `Promise`\<`Result`\> | A function that outputs a Promise result. Typically, the operation does not use its arguments. |
|
|
1674
|
+
| `backoff` | `number`[] \| (`attempt`, `previousResult`, `previousError`) => `number` \| `undefined` | An array of retry backoff periods (in milliseconds) or a function for calculating them. If retry is desired, the specified backoff period is waited before the next call to the operation. If the array runs out of elements or the function returns `undefined` or a negative number, no further calls to the operation will be made. The `attempt` argument passed to the backoff function starts from 1, as it is called immediately after the first attempt and before the first retry. |
|
|
1675
|
+
| `shouldRetry` | (`previousError`, `previousResult`, `attempt`) => `boolean` | A predicate function for deciding whether another call to the operation should occur. If this argument is not defined, a retry will occur whenever the operation rejects with an error. The `shouldRetry` function is evaluated before the `backoff`. The `attempt` argument passed to the shouldRetry function starts from 1. |
|
|
1174
1676
|
|
|
1175
|
-
|
|
1677
|
+
#### Returns
|
|
1176
1678
|
|
|
1177
1679
|
`Promise`\<`Result`\>
|
|
1178
1680
|
|
|
1179
1681
|
A promise of the operation result, potentially with retries applied.
|
|
1180
1682
|
|
|
1181
|
-
|
|
1683
|
+
#### Example
|
|
1182
1684
|
|
|
1183
1685
|
```ts
|
|
1184
1686
|
const result = await PromiseUtils.withRetry(() => doSomething(), [100, 200, 300, 500, 800, 1000]);
|
|
@@ -1186,30 +1688,19 @@ const result2 = await PromiseUtils.withRetry(() => doSomething(), Array.from({le
|
|
|
1186
1688
|
const result3 = await PromiseUtils.withRetry(() => doSomething(), attempt => attempt <= 8 ? 1000 * Math.min(FIBONACCI_SEQUENCE[attempt - 1], 10) : undefined, err => err.statusCode === 429);
|
|
1187
1689
|
```
|
|
1188
1690
|
|
|
1189
|
-
|
|
1691
|
+
#### Param
|
|
1190
1692
|
|
|
1693
|
+
A function that outputs a Promise result. Typically, the operation does not use its arguments.
|
|
1191
1694
|
|
|
1192
|
-
|
|
1695
|
+
#### Param
|
|
1193
1696
|
|
|
1194
|
-
|
|
1697
|
+
An array of retry backoff periods (in milliseconds) or a function for calculating them.
|
|
1195
1698
|
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
#### Enumeration Members
|
|
1199
|
-
|
|
1200
|
-
##### Fulfilled
|
|
1201
|
-
|
|
1202
|
-
• **Fulfilled** = ``"Fulfilled"``
|
|
1699
|
+
#### Param
|
|
1203
1700
|
|
|
1204
|
-
|
|
1701
|
+
A predicate function for deciding whether another call to the operation should occur.
|
|
1205
1702
|
|
|
1206
|
-
|
|
1703
|
+
#### Returns
|
|
1207
1704
|
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
___
|
|
1211
|
-
|
|
1212
|
-
##### Rejected
|
|
1213
|
-
|
|
1214
|
-
• **Rejected** = ``"Rejected"``
|
|
1705
|
+
A promise of the operation result, potentially with retries applied.
|
|
1215
1706
|
<!-- API end -->
|