@handy-common-utils/promise-utils 1.2.7 → 1.3.0
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 +324 -119
- package/dist/promise-utils.d.ts +25 -23
- package/dist/promise-utils.d.ts.map +1 -1
- package/dist/promise-utils.js +36 -29
- package/package.json +14 -9
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @handy-common-utils/promise-utils
|
|
2
2
|
|
|
3
3
|
These Promise related utilities have 100% test coverage. The package is tiny because there is no dependency on any other package.
|
|
4
|
-
Functions provided are `repeat`, `withRetry`, `inParallel`, `delayedResolve`, `delayedReject`, `
|
|
4
|
+
Functions provided are `repeat`, `withRetry`, `inParallel`, `delayedResolve`, `delayedReject`, `timeoutResolve`, `timeoutReject`, `promiseState`, `synchronized`, etc.
|
|
5
5
|
|
|
6
6
|
[](https://npmjs.org/package/@handy-common-utils/promise-utils)
|
|
7
7
|
[](https://npmjs.org/package/@handy-common-utils/promise-utils)
|
|
@@ -76,38 +76,16 @@ await inParallel(5, topicArns, async topicArn => {
|
|
|
76
76
|
<!-- API start -->
|
|
77
77
|
<a name="readmemd"></a>
|
|
78
78
|
|
|
79
|
-
@handy-common-utils/promise-utils
|
|
80
|
-
|
|
81
79
|
## @handy-common-utils/promise-utils
|
|
82
80
|
|
|
83
|
-
###
|
|
84
|
-
|
|
85
|
-
#### Enumerations
|
|
81
|
+
### Enumerations
|
|
86
82
|
|
|
87
83
|
- [PromiseState](#enumspromisestatemd)
|
|
88
84
|
|
|
89
|
-
|
|
85
|
+
### Classes
|
|
90
86
|
|
|
91
87
|
- [PromiseUtils](#classespromiseutilsmd)
|
|
92
88
|
|
|
93
|
-
#### Variables
|
|
94
|
-
|
|
95
|
-
- [EXPONENTIAL\_SEQUENCE](#exponential_sequence)
|
|
96
|
-
- [FIBONACCI\_SEQUENCE](#fibonacci_sequence)
|
|
97
|
-
|
|
98
|
-
#### Functions
|
|
99
|
-
|
|
100
|
-
- [delayedReject](#delayedreject)
|
|
101
|
-
- [delayedResolve](#delayedresolve)
|
|
102
|
-
- [inParallel](#inparallel)
|
|
103
|
-
- [promiseState](#promisestate)
|
|
104
|
-
- [repeat](#repeat)
|
|
105
|
-
- [synchronised](#synchronised)
|
|
106
|
-
- [synchronized](#synchronized)
|
|
107
|
-
- [timeoutReject](#timeoutreject)
|
|
108
|
-
- [timeoutResolve](#timeoutresolve)
|
|
109
|
-
- [withRetry](#withretry)
|
|
110
|
-
|
|
111
89
|
### Variables
|
|
112
90
|
|
|
113
91
|
#### EXPONENTIAL\_SEQUENCE
|
|
@@ -117,19 +95,19 @@ await inParallel(5, topicArns, async topicArn => {
|
|
|
117
95
|
Array of 25 exponential numbers starting from 1 up to 33554432.
|
|
118
96
|
It can be used to form your own backoff interval array.
|
|
119
97
|
|
|
120
|
-
**`
|
|
121
|
-
```javascript
|
|
98
|
+
**`Example`**
|
|
122
99
|
|
|
100
|
+
```ts
|
|
123
101
|
// 1ms, 2ms, 4ms, 8ms, 16ms, 32ms
|
|
124
102
|
PromiseUtils.withRetry(() => doSomething(), EXPONENTIAL_SEQUENCE.slice(0, 5), err => err.statusCode === 429);
|
|
125
103
|
// 1s, 2s, 4s, 8s, 10s, 10s, 10s, 10s, 10s, 10s
|
|
126
104
|
PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(EXPONENTIAL_SEQUENCE[i], 10)), err => err.statusCode === 429);
|
|
127
105
|
// with +-10% randomness: 1s, 2s, 4s, 8s
|
|
128
106
|
PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 4).map(n => 1000 * n * (1 + (Math.random() - 0.5) / 5)), err => err.statusCode === 429);
|
|
107
|
+
```
|
|
129
108
|
|
|
130
109
|
___
|
|
131
110
|
|
|
132
|
-
```
|
|
133
111
|
#### FIBONACCI\_SEQUENCE
|
|
134
112
|
|
|
135
113
|
• `Const` **FIBONACCI\_SEQUENCE**: `number`[]
|
|
@@ -137,43 +115,280 @@ ___
|
|
|
137
115
|
Array of 25 Fibonacci numbers starting from 1 up to 317811.
|
|
138
116
|
It can be used to form your own backoff interval array.
|
|
139
117
|
|
|
140
|
-
**`
|
|
141
|
-
```javascript
|
|
118
|
+
**`Example`**
|
|
142
119
|
|
|
120
|
+
```ts
|
|
143
121
|
// 1ms, 2ms, 3ms, 5ms, 8ms, 13ms
|
|
144
122
|
PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 5), err => err.statusCode === 429);
|
|
145
123
|
// 1s, 2s, 3s, 4s, 8s, 10s, 10s, 10s, 10s, 10s
|
|
146
124
|
PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(FIBONACCI_SEQUENCE[i], 10)), err => err.statusCode === 429);
|
|
147
125
|
// with +-10% randomness: 1s, 2s, 3s, 5s, 8s, 13s
|
|
148
126
|
PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 5).map(n => 1000 * n * (1 + (Math.random() - 0.5) / 5)), err => err.statusCode === 429);
|
|
127
|
+
```
|
|
149
128
|
|
|
150
|
-
|
|
129
|
+
### Functions
|
|
151
130
|
|
|
131
|
+
#### delayedReject
|
|
152
132
|
|
|
153
|
-
|
|
133
|
+
▸ **delayedReject**<`T`, `R`\>(`ms`, `reason`): `Promise`<`T`\>
|
|
154
134
|
|
|
155
|
-
[
|
|
135
|
+
See [delayedReject](#delayedreject) for full documentation.
|
|
156
136
|
|
|
157
|
-
|
|
137
|
+
##### Type parameters
|
|
138
|
+
|
|
139
|
+
| Name | Type |
|
|
140
|
+
| :------ | :------ |
|
|
141
|
+
| `T` | `never` |
|
|
142
|
+
| `R` | `any` |
|
|
143
|
+
|
|
144
|
+
##### Parameters
|
|
145
|
+
|
|
146
|
+
| Name | Type |
|
|
147
|
+
| :------ | :------ |
|
|
148
|
+
| `ms` | `number` |
|
|
149
|
+
| `reason` | `R` \| `PromiseLike`<`R`\> \| () => `R` \| `PromiseLike`<`R`\> |
|
|
150
|
+
|
|
151
|
+
##### Returns
|
|
152
|
+
|
|
153
|
+
`Promise`<`T`\>
|
|
154
|
+
|
|
155
|
+
___
|
|
156
|
+
|
|
157
|
+
#### delayedResolve
|
|
158
|
+
|
|
159
|
+
▸ **delayedResolve**<`T`\>(`ms`, `result?`): `Promise`<`T`\>
|
|
158
160
|
|
|
159
|
-
|
|
161
|
+
See [delayedResolve](#delayedresolve) for full documentation.
|
|
160
162
|
|
|
161
|
-
#####
|
|
163
|
+
##### Type parameters
|
|
164
|
+
|
|
165
|
+
| Name |
|
|
166
|
+
| :------ |
|
|
167
|
+
| `T` |
|
|
168
|
+
|
|
169
|
+
##### Parameters
|
|
170
|
+
|
|
171
|
+
| Name | Type |
|
|
172
|
+
| :------ | :------ |
|
|
173
|
+
| `ms` | `number` |
|
|
174
|
+
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> |
|
|
175
|
+
|
|
176
|
+
##### Returns
|
|
177
|
+
|
|
178
|
+
`Promise`<`T`\>
|
|
179
|
+
|
|
180
|
+
___
|
|
181
|
+
|
|
182
|
+
#### inParallel
|
|
183
|
+
|
|
184
|
+
▸ **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`): `Promise`<(`Result` \| `TError`)[]\>
|
|
185
|
+
|
|
186
|
+
See [inParallel](#inparallel) for full documentation.
|
|
187
|
+
|
|
188
|
+
##### Type parameters
|
|
189
|
+
|
|
190
|
+
| Name | Type |
|
|
191
|
+
| :------ | :------ |
|
|
192
|
+
| `Data` | `Data` |
|
|
193
|
+
| `Result` | `Result` |
|
|
194
|
+
| `TError` | `Result` |
|
|
195
|
+
|
|
196
|
+
##### Parameters
|
|
197
|
+
|
|
198
|
+
| Name | Type |
|
|
199
|
+
| :------ | :------ |
|
|
200
|
+
| `parallelism` | `number` |
|
|
201
|
+
| `jobs` | `Iterable`<`Data`\> |
|
|
202
|
+
| `operation` | (`job`: `Data`, `index`: `number`) => `Promise`<`Result`\> |
|
|
203
|
+
|
|
204
|
+
##### Returns
|
|
205
|
+
|
|
206
|
+
`Promise`<(`Result` \| `TError`)[]\>
|
|
207
|
+
|
|
208
|
+
___
|
|
162
209
|
|
|
163
|
-
|
|
210
|
+
#### promiseState
|
|
164
211
|
|
|
165
|
-
|
|
212
|
+
▸ **promiseState**(`p`): `Promise`<[`PromiseState`](#enumspromisestatemd)\>
|
|
166
213
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
214
|
+
See [promiseState](#promisestate) for full documentation.
|
|
215
|
+
|
|
216
|
+
##### Parameters
|
|
217
|
+
|
|
218
|
+
| Name | Type |
|
|
219
|
+
| :------ | :------ |
|
|
220
|
+
| `p` | `Promise`<`any`\> |
|
|
221
|
+
|
|
222
|
+
##### Returns
|
|
223
|
+
|
|
224
|
+
`Promise`<[`PromiseState`](#enumspromisestatemd)\>
|
|
225
|
+
|
|
226
|
+
___
|
|
227
|
+
|
|
228
|
+
#### repeat
|
|
229
|
+
|
|
230
|
+
▸ **repeat**<`Result`, `Param`, `Collection`\>(`operation`, `nextParameter`, `collect`, `initialCollection`, `initialParameter?`): `Promise`<`Collection`\>
|
|
231
|
+
|
|
232
|
+
See [repeat](#repeat) for full documentation.
|
|
233
|
+
|
|
234
|
+
##### Type parameters
|
|
235
|
+
|
|
236
|
+
| Name |
|
|
237
|
+
| :------ |
|
|
238
|
+
| `Result` |
|
|
239
|
+
| `Param` |
|
|
240
|
+
| `Collection` |
|
|
241
|
+
|
|
242
|
+
##### Parameters
|
|
243
|
+
|
|
244
|
+
| Name | Type |
|
|
245
|
+
| :------ | :------ |
|
|
246
|
+
| `operation` | (`parameter`: `Partial`<`Param`\>) => `Promise`<`Result`\> |
|
|
247
|
+
| `nextParameter` | (`response`: `Result`) => ``null`` \| `Partial`<`Param`\> \| `Promise`<`Partial`<`Param`\>\> |
|
|
248
|
+
| `collect` | (`collection`: `Collection`, `result`: `Result`) => `Collection` |
|
|
249
|
+
| `initialCollection` | `Collection` |
|
|
250
|
+
| `initialParameter` | `Partial`<`Param`\> |
|
|
251
|
+
|
|
252
|
+
##### Returns
|
|
253
|
+
|
|
254
|
+
`Promise`<`Collection`\>
|
|
255
|
+
|
|
256
|
+
___
|
|
257
|
+
|
|
258
|
+
#### synchronised
|
|
259
|
+
|
|
260
|
+
▸ **synchronised**<`T`\>(`lock`, `operation`): `Promise`<`T`\>
|
|
261
|
+
|
|
262
|
+
See [synchronised](#synchronised) for full documentation.
|
|
263
|
+
|
|
264
|
+
##### Type parameters
|
|
265
|
+
|
|
266
|
+
| Name |
|
|
267
|
+
| :------ |
|
|
268
|
+
| `T` |
|
|
269
|
+
|
|
270
|
+
##### Parameters
|
|
271
|
+
|
|
272
|
+
| Name | Type |
|
|
273
|
+
| :------ | :------ |
|
|
274
|
+
| `lock` | `unknown` |
|
|
275
|
+
| `operation` | (`previousState`: `undefined` \| [`PromiseState`](#enumspromisestatemd), `previousSettledState`: `undefined` \| [`PromiseState`](#enumspromisestatemd), `previousResult`: `any`) => `Promise`<`T`\> |
|
|
276
|
+
|
|
277
|
+
##### Returns
|
|
278
|
+
|
|
279
|
+
`Promise`<`T`\>
|
|
280
|
+
|
|
281
|
+
___
|
|
282
|
+
|
|
283
|
+
#### synchronized
|
|
284
|
+
|
|
285
|
+
▸ **synchronized**<`T`\>(`lock`, `operation`): `Promise`<`T`\>
|
|
286
|
+
|
|
287
|
+
See [synchronized](#synchronized) for full documentation.
|
|
288
|
+
|
|
289
|
+
##### Type parameters
|
|
290
|
+
|
|
291
|
+
| Name |
|
|
292
|
+
| :------ |
|
|
293
|
+
| `T` |
|
|
294
|
+
|
|
295
|
+
##### Parameters
|
|
296
|
+
|
|
297
|
+
| Name | Type |
|
|
298
|
+
| :------ | :------ |
|
|
299
|
+
| `lock` | `unknown` |
|
|
300
|
+
| `operation` | (`previousState`: `undefined` \| [`PromiseState`](#enumspromisestatemd), `previousSettledState`: `undefined` \| [`PromiseState`](#enumspromisestatemd), `previousResult`: `any`) => `Promise`<`T`\> |
|
|
301
|
+
|
|
302
|
+
##### Returns
|
|
303
|
+
|
|
304
|
+
`Promise`<`T`\>
|
|
305
|
+
|
|
306
|
+
___
|
|
307
|
+
|
|
308
|
+
#### timeoutReject
|
|
309
|
+
|
|
310
|
+
▸ **timeoutReject**<`T`, `R`\>(`operation`, `ms`, `rejectReason`): `Promise`<`T`\>
|
|
311
|
+
|
|
312
|
+
See [timeoutReject](#timeoutreject) for full documentation.
|
|
313
|
+
|
|
314
|
+
##### Type parameters
|
|
315
|
+
|
|
316
|
+
| Name | Type |
|
|
317
|
+
| :------ | :------ |
|
|
318
|
+
| `T` | `never` |
|
|
319
|
+
| `R` | `any` |
|
|
320
|
+
|
|
321
|
+
##### Parameters
|
|
322
|
+
|
|
323
|
+
| Name | Type |
|
|
324
|
+
| :------ | :------ |
|
|
325
|
+
| `operation` | `Promise`<`T`\> \| () => `Promise`<`T`\> |
|
|
326
|
+
| `ms` | `number` |
|
|
327
|
+
| `rejectReason` | `R` \| `PromiseLike`<`R`\> \| () => `R` \| `PromiseLike`<`R`\> |
|
|
328
|
+
|
|
329
|
+
##### Returns
|
|
330
|
+
|
|
331
|
+
`Promise`<`T`\>
|
|
332
|
+
|
|
333
|
+
___
|
|
334
|
+
|
|
335
|
+
#### timeoutResolve
|
|
336
|
+
|
|
337
|
+
▸ **timeoutResolve**<`T`\>(`operation`, `ms`, `result?`): `Promise`<`T`\>
|
|
338
|
+
|
|
339
|
+
See [timeoutResolve](#timeoutresolve) for full documentation.
|
|
340
|
+
|
|
341
|
+
##### Type parameters
|
|
342
|
+
|
|
343
|
+
| Name |
|
|
344
|
+
| :------ |
|
|
345
|
+
| `T` |
|
|
346
|
+
|
|
347
|
+
##### Parameters
|
|
348
|
+
|
|
349
|
+
| Name | Type |
|
|
350
|
+
| :------ | :------ |
|
|
351
|
+
| `operation` | `Promise`<`T`\> \| () => `Promise`<`T`\> |
|
|
352
|
+
| `ms` | `number` |
|
|
353
|
+
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> |
|
|
354
|
+
|
|
355
|
+
##### Returns
|
|
356
|
+
|
|
357
|
+
`Promise`<`T`\>
|
|
358
|
+
|
|
359
|
+
___
|
|
360
|
+
|
|
361
|
+
#### withRetry
|
|
362
|
+
|
|
363
|
+
▸ **withRetry**<`Result`, `TError`\>(`operation`, `backoff`, `shouldRetry?`): `Promise`<`Result`\>
|
|
364
|
+
|
|
365
|
+
See [withRetry](#withretry) for full documentation.
|
|
366
|
+
|
|
367
|
+
##### Type parameters
|
|
368
|
+
|
|
369
|
+
| Name | Type |
|
|
370
|
+
| :------ | :------ |
|
|
371
|
+
| `Result` | `Result` |
|
|
372
|
+
| `TError` | `any` |
|
|
373
|
+
|
|
374
|
+
##### Parameters
|
|
375
|
+
|
|
376
|
+
| Name | Type |
|
|
377
|
+
| :------ | :------ |
|
|
378
|
+
| `operation` | (`attempt`: `number`, `previousResult`: `undefined` \| `Result`, `previousError`: `undefined` \| `TError`) => `Promise`<`Result`\> |
|
|
379
|
+
| `backoff` | `number`[] \| (`attempt`: `number`, `previousResult`: `undefined` \| `Result`, `previousError`: `undefined` \| `TError`) => `undefined` \| `number` |
|
|
380
|
+
| `shouldRetry` | (`previousError`: `undefined` \| `TError`, `previousResult`: `undefined` \| `Result`, `attempt`: `number`) => `boolean` |
|
|
381
|
+
|
|
382
|
+
##### Returns
|
|
383
|
+
|
|
384
|
+
`Promise`<`Result`\>
|
|
385
|
+
|
|
386
|
+
## Classes
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
<a name="classespromiseutilsmd"></a>
|
|
390
|
+
|
|
391
|
+
### Class: PromiseUtils
|
|
177
392
|
|
|
178
393
|
#### Constructors
|
|
179
394
|
|
|
@@ -228,7 +443,7 @@ Create a Promise that resolves after number of milliseconds specified
|
|
|
228
443
|
| Name | Type | Description |
|
|
229
444
|
| :------ | :------ | :------ |
|
|
230
445
|
| `ms` | `number` | number of milliseconds after which the created Promise would resolve |
|
|
231
|
-
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> | the result to be resolved for the Promise, or a function that supplies the
|
|
446
|
+
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> | the result to be resolved for the Promise, or a function that supplies the result. |
|
|
232
447
|
|
|
233
448
|
###### Returns
|
|
234
449
|
|
|
@@ -244,19 +459,6 @@ ___
|
|
|
244
459
|
|
|
245
460
|
Run multiple jobs/operations in parallel.
|
|
246
461
|
|
|
247
|
-
**`example`**
|
|
248
|
-
```javascript
|
|
249
|
-
|
|
250
|
-
const topicArns = topics.map(topic => topic.TopicArn!);
|
|
251
|
-
await PromiseUtils.inParallel(5, topicArns, async topicArn => {
|
|
252
|
-
const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
|
|
253
|
-
const topicDetails = { ...topicAttributes, subscriptions: [] } as any;
|
|
254
|
-
if (this.shouldInclude(topicArn)) {
|
|
255
|
-
inventory.snsTopicsByArn.set(topicArn, topicDetails);
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
```
|
|
260
462
|
###### Type parameters
|
|
261
463
|
|
|
262
464
|
| Name | Type | Description |
|
|
@@ -270,7 +472,7 @@ await PromiseUtils.inParallel(5, topicArns, async topicArn => {
|
|
|
270
472
|
| Name | Type | Description |
|
|
271
473
|
| :------ | :------ | :------ |
|
|
272
474
|
| `parallelism` | `number` | how many jobs/operations can be running at the same time |
|
|
273
|
-
| `jobs` | `Iterable`<`Data`\> | job data which will be the input to operation function.
|
|
475
|
+
| `jobs` | `Iterable`<`Data`\> | job data which will be the input to operation function. This function is safe when there are infinite unknown number of elements in the job data. |
|
|
274
476
|
| `operation` | (`job`: `Data`, `index`: `number`) => `Promise`<`Result`\> | the function that turns job data into result asynchronously |
|
|
275
477
|
|
|
276
478
|
###### Returns
|
|
@@ -281,6 +483,19 @@ Promise of void if the operation function does not return a value,
|
|
|
281
483
|
or promise of an array containing results returned from the operation function.
|
|
282
484
|
In the array containing results, each element is either the fulfilled result, or the rejected error/reason.
|
|
283
485
|
|
|
486
|
+
**`Example`**
|
|
487
|
+
|
|
488
|
+
```ts
|
|
489
|
+
const topicArns = topics.map(topic => topic.TopicArn!);
|
|
490
|
+
await PromiseUtils.inParallel(5, topicArns, async topicArn => {
|
|
491
|
+
const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
|
|
492
|
+
const topicDetails = { ...topicAttributes, subscriptions: [] } as any;
|
|
493
|
+
if (this.shouldInclude(topicArn)) {
|
|
494
|
+
inventory.snsTopicsByArn.set(topicArn, topicDetails);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
```
|
|
498
|
+
|
|
284
499
|
___
|
|
285
500
|
|
|
286
501
|
##### promiseState
|
|
@@ -300,7 +515,7 @@ Please note that the returned value is a Promise, although it resolves immediate
|
|
|
300
515
|
|
|
301
516
|
`Promise`<[`PromiseState`](#enumspromisestatemd)\>
|
|
302
517
|
|
|
303
|
-
A Promise that resolves immediately
|
|
518
|
+
A Promise that resolves immediately containing the state of the input Promise
|
|
304
519
|
|
|
305
520
|
___
|
|
306
521
|
|
|
@@ -311,17 +526,6 @@ ___
|
|
|
311
526
|
Do an operation repeatedly and collect all the results.
|
|
312
527
|
This function is useful for client side pagination.
|
|
313
528
|
|
|
314
|
-
**`example`**
|
|
315
|
-
```javascript
|
|
316
|
-
|
|
317
|
-
const domainNameObjects = await PromiseUtils.repeat(
|
|
318
|
-
pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(),
|
|
319
|
-
esponse => response.position? {position: response.position} : null,
|
|
320
|
-
(collection, response) => collection.concat(response.items!),
|
|
321
|
-
[] as APIGateway.DomainName[],
|
|
322
|
-
);
|
|
323
|
-
|
|
324
|
-
```
|
|
325
529
|
###### Type parameters
|
|
326
530
|
|
|
327
531
|
| Name | Description |
|
|
@@ -335,7 +539,7 @@ const domainNameObjects = await PromiseUtils.repeat(
|
|
|
335
539
|
| Name | Type | Description |
|
|
336
540
|
| :------ | :------ | :------ |
|
|
337
541
|
| `operation` | (`parameter`: `Partial`<`Param`\>) => `Promise`<`Result`\> | a function that takes paging parameter as input and outputs a result, normally the operation supports paging |
|
|
338
|
-
| `nextParameter` | (`response`: `Result`) => ``null`` \| `Partial`<`Param`\> \| `Promise`<`Partial`<`Param`\>\> | The function for calculating next parameter from the operation result.
|
|
542
|
+
| `nextParameter` | (`response`: `Result`) => ``null`` \| `Partial`<`Param`\> \| `Promise`<`Partial`<`Param`\>\> | The function for calculating next parameter from the operation result. Normally the parameter controls paging, This function should return null when next invocation of the operation function is not desired. If next invocation is desired, the return value of this function can be a Promise or not a Promise. |
|
|
339
543
|
| `collect` | (`collection`: `Collection`, `result`: `Result`) => `Collection` | the function for merging operation result into the collection |
|
|
340
544
|
| `initialCollection` | `Collection` | initial collection which would be the first argument passed into the first invocation of the collect function |
|
|
341
545
|
| `initialParameter` | `Partial`<`Param`\> | the parameter for the first operation |
|
|
@@ -346,13 +550,24 @@ const domainNameObjects = await PromiseUtils.repeat(
|
|
|
346
550
|
|
|
347
551
|
Promise of collection of all the results returned by the operation function
|
|
348
552
|
|
|
553
|
+
**`Example`**
|
|
554
|
+
|
|
555
|
+
```ts
|
|
556
|
+
const domainNameObjects = await PromiseUtils.repeat(
|
|
557
|
+
pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(),
|
|
558
|
+
response => response.position? {position: response.position} : null,
|
|
559
|
+
(collection, response) => collection.concat(response.items!),
|
|
560
|
+
[] as APIGateway.DomainName[],
|
|
561
|
+
);
|
|
562
|
+
```
|
|
563
|
+
|
|
349
564
|
___
|
|
350
565
|
|
|
351
566
|
##### synchronised
|
|
352
567
|
|
|
353
568
|
▸ `Static` **synchronised**<`T`\>(`lock`, `operation`): `Promise`<`T`\>
|
|
354
569
|
|
|
355
|
-
This is just another spelling of [
|
|
570
|
+
This is just another spelling of [synchronized](#synchronized).
|
|
356
571
|
|
|
357
572
|
###### Type parameters
|
|
358
573
|
|
|
@@ -410,11 +625,11 @@ ___
|
|
|
410
625
|
|
|
411
626
|
▸ `Static` **timeoutReject**<`T`, `R`\>(`operation`, `ms`, `rejectReason`): `Promise`<`T`\>
|
|
412
627
|
|
|
413
|
-
|
|
414
|
-
If timeout
|
|
415
|
-
If timeout
|
|
416
|
-
|
|
417
|
-
|
|
628
|
+
Applies a timeout to a Promise or a function that returns a Promise.
|
|
629
|
+
If the timeout occurs, rejects with the specified reason.
|
|
630
|
+
If the timeout doesn't occur, the resolved result or rejection reason of the original Promise will be the outcome of the Promise returned from this function.
|
|
631
|
+
If the 'reason' parameter is a function and timeout doesn't occur, the function won't be called.
|
|
632
|
+
The rejection of the 'operation' parameter is not handled by this function, you may want to handle it outside of this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously".
|
|
418
633
|
|
|
419
634
|
###### Type parameters
|
|
420
635
|
|
|
@@ -427,15 +642,15 @@ just for avoiding warnings like "(node:4330) PromiseRejectionHandledWarning: Pro
|
|
|
427
642
|
|
|
428
643
|
| Name | Type | Description |
|
|
429
644
|
| :------ | :------ | :------ |
|
|
430
|
-
| `operation` | `Promise`<`T`\> |
|
|
431
|
-
| `ms` | `number` | number of milliseconds for the timeout |
|
|
432
|
-
| `rejectReason` | `R` \| `PromiseLike`<`R`\> \| () => `R` \| `PromiseLike`<`R`\> |
|
|
645
|
+
| `operation` | `Promise`<`T`\> \| () => `Promise`<`T`\> | The original Promise or a function that returns a Promise for which the timeout will be applied. |
|
|
646
|
+
| `ms` | `number` | The number of milliseconds for the timeout. |
|
|
647
|
+
| `rejectReason` | `R` \| `PromiseLike`<`R`\> \| () => `R` \| `PromiseLike`<`R`\> | The reason to reject with if the timeout occurs, or a function that supplies the reason. |
|
|
433
648
|
|
|
434
649
|
###### Returns
|
|
435
650
|
|
|
436
651
|
`Promise`<`T`\>
|
|
437
652
|
|
|
438
|
-
|
|
653
|
+
A new Promise that rejects with the specified reason if the timeout occurs.
|
|
439
654
|
|
|
440
655
|
___
|
|
441
656
|
|
|
@@ -443,11 +658,11 @@ ___
|
|
|
443
658
|
|
|
444
659
|
▸ `Static` **timeoutResolve**<`T`\>(`operation`, `ms`, `result?`): `Promise`<`T`\>
|
|
445
660
|
|
|
446
|
-
|
|
447
|
-
If timeout
|
|
448
|
-
If timeout
|
|
449
|
-
|
|
450
|
-
|
|
661
|
+
Applies a timeout to a Promise or a function that returns a Promise.
|
|
662
|
+
If the timeout occurs, resolves to the specified result.
|
|
663
|
+
If the timeout doesn't occur, the resolved result or rejection reason of the original Promise will be the outcome of the Promise returned from this function.
|
|
664
|
+
If the 'result' parameter is a function and timeout doesn't occur, the function won't be called.
|
|
665
|
+
The rejection of the 'operation' parameter is not handled by this function, you may want to handle it outside of this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously".
|
|
451
666
|
|
|
452
667
|
###### Type parameters
|
|
453
668
|
|
|
@@ -459,15 +674,15 @@ just for avoiding warnings like "(node:4330) PromiseRejectionHandledWarning: Pro
|
|
|
459
674
|
|
|
460
675
|
| Name | Type | Description |
|
|
461
676
|
| :------ | :------ | :------ |
|
|
462
|
-
| `operation` | `Promise`<`T`\> |
|
|
463
|
-
| `ms` | `number` | number of milliseconds for the timeout |
|
|
464
|
-
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> |
|
|
677
|
+
| `operation` | `Promise`<`T`\> \| () => `Promise`<`T`\> | The original Promise or a function that returns a Promise for which the timeout will be applied. |
|
|
678
|
+
| `ms` | `number` | The number of milliseconds for the timeout. |
|
|
679
|
+
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> | The result to be resolved with if the timeout occurs, or a function that supplies the result. |
|
|
465
680
|
|
|
466
681
|
###### Returns
|
|
467
682
|
|
|
468
683
|
`Promise`<`T`\>
|
|
469
684
|
|
|
470
|
-
|
|
685
|
+
A new Promise that resolves to the specified result if the timeout occurs.
|
|
471
686
|
|
|
472
687
|
___
|
|
473
688
|
|
|
@@ -477,14 +692,6 @@ ___
|
|
|
477
692
|
|
|
478
693
|
Do an operation repeatedly until a criteria is met.
|
|
479
694
|
|
|
480
|
-
**`example`**
|
|
481
|
-
```javascript
|
|
482
|
-
|
|
483
|
-
const result = await PromiseUtils.withRetry(() => doSomething(), [100, 200, 300, 500, 800, 1000]);
|
|
484
|
-
const result2 = await PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(FIBONACCI_SEQUENCE[i], 10), err => err.statusCode === 429);
|
|
485
|
-
const result3 = await PromiseUtils.withRetry(() => doSomething(), attempt => attempt <= 8 ? 1000 * Math.min(FIBONACCI_SEQUENCE[attempt - 1], 10) : undefined, err => err.statusCode === 429);
|
|
486
|
-
|
|
487
|
-
```
|
|
488
695
|
###### Type parameters
|
|
489
696
|
|
|
490
697
|
| Name | Type | Description |
|
|
@@ -497,8 +704,8 @@ const result3 = await PromiseUtils.withRetry(() => doSomething(), attempt => att
|
|
|
497
704
|
| Name | Type | Description |
|
|
498
705
|
| :------ | :------ | :------ |
|
|
499
706
|
| `operation` | (`attempt`: `number`, `previousResult`: `undefined` \| `Result`, `previousError`: `undefined` \| `TError`) => `Promise`<`Result`\> | a function that outputs a Promise result, normally the operation does not use its arguments |
|
|
500
|
-
| `backoff` | `number`[] \| (`attempt`: `number`, `previousResult`: `undefined` \| `Result`, `previousError`: `undefined` \| `TError`) => `undefined` \| `number` | Array of retry backoff periods (unit: milliseconds) or function for calculating them.
|
|
501
|
-
| `shouldRetry` | (`previousError`: `undefined` \| `TError`, `previousResult`: `undefined` \| `Result`, `attempt`: `number`) => `boolean` | Predicate function for deciding whether another call to the operation should happen.
|
|
707
|
+
| `backoff` | `number`[] \| (`attempt`: `number`, `previousResult`: `undefined` \| `Result`, `previousError`: `undefined` \| `TError`) => `undefined` \| `number` | Array of retry backoff periods (unit: milliseconds) or function for calculating them. If retry is desired, before making next call to the operation the desired backoff period would be waited. If the array runs out of elements or the function returns `undefined` or either the array or the function returns a negative number, there would be no further call to the operation. The `attempt` argument passed into backoff function starts from 1 because the function is called right after the first attempt and before the first retry. |
|
|
708
|
+
| `shouldRetry` | (`previousError`: `undefined` \| `TError`, `previousResult`: `undefined` \| `Result`, `attempt`: `number`) => `boolean` | Predicate function for deciding whether another call to the operation should happen. If this argument is not defined, retry would happen whenever the operation rejects with an error. `shouldRetry` would be evaluated before `backoff`. The `attempt` argument passed into shouldRetry function starts from 1. |
|
|
502
709
|
|
|
503
710
|
###### Returns
|
|
504
711
|
|
|
@@ -506,40 +713,38 @@ const result3 = await PromiseUtils.withRetry(() => doSomething(), attempt => att
|
|
|
506
713
|
|
|
507
714
|
Promise of the operation result potentially with retries already applied
|
|
508
715
|
|
|
716
|
+
**`Example`**
|
|
717
|
+
|
|
718
|
+
```ts
|
|
719
|
+
const result = await PromiseUtils.withRetry(() => doSomething(), [100, 200, 300, 500, 800, 1000]);
|
|
720
|
+
const result2 = await PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(FIBONACCI_SEQUENCE[i], 10), err => err.statusCode === 429);
|
|
721
|
+
const result3 = await PromiseUtils.withRetry(() => doSomething(), attempt => attempt <= 8 ? 1000 * Math.min(FIBONACCI_SEQUENCE[attempt - 1], 10) : undefined, err => err.statusCode === 429);
|
|
722
|
+
```
|
|
723
|
+
|
|
509
724
|
## Enums
|
|
510
725
|
|
|
511
726
|
|
|
512
727
|
<a name="enumspromisestatemd"></a>
|
|
513
728
|
|
|
514
|
-
[@handy-common-utils/promise-utils](#readmemd) / PromiseState
|
|
515
|
-
|
|
516
729
|
### Enumeration: PromiseState
|
|
517
730
|
|
|
518
731
|
The state of a Promise can only be one of: Pending, Fulfilled, and Rejected.
|
|
519
732
|
|
|
520
|
-
####
|
|
521
|
-
|
|
522
|
-
##### Enumeration members
|
|
523
|
-
|
|
524
|
-
- [Fulfilled](#fulfilled)
|
|
525
|
-
- [Pending](#pending)
|
|
526
|
-
- [Rejected](#rejected)
|
|
527
|
-
|
|
528
|
-
#### Enumeration members
|
|
733
|
+
#### Enumeration Members
|
|
529
734
|
|
|
530
735
|
##### Fulfilled
|
|
531
736
|
|
|
532
|
-
• **Fulfilled** =
|
|
737
|
+
• **Fulfilled** = ``"Fulfilled"``
|
|
533
738
|
|
|
534
739
|
___
|
|
535
740
|
|
|
536
741
|
##### Pending
|
|
537
742
|
|
|
538
|
-
• **Pending** =
|
|
743
|
+
• **Pending** = ``"Pending"``
|
|
539
744
|
|
|
540
745
|
___
|
|
541
746
|
|
|
542
747
|
##### Rejected
|
|
543
748
|
|
|
544
|
-
• **Rejected** =
|
|
749
|
+
• **Rejected** = ``"Rejected"``
|
|
545
750
|
<!-- API end -->
|
package/dist/promise-utils.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export declare abstract class PromiseUtils {
|
|
|
38
38
|
* @example
|
|
39
39
|
* const domainNameObjects = await PromiseUtils.repeat(
|
|
40
40
|
* pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(),
|
|
41
|
-
*
|
|
41
|
+
* response => response.position? {position: response.position} : null,
|
|
42
42
|
* (collection, response) => collection.concat(response.items!),
|
|
43
43
|
* [] as APIGateway.DomainName[],
|
|
44
44
|
* );
|
|
@@ -111,7 +111,7 @@ export declare abstract class PromiseUtils {
|
|
|
111
111
|
/**
|
|
112
112
|
* Create a Promise that resolves after number of milliseconds specified
|
|
113
113
|
* @param ms number of milliseconds after which the created Promise would resolve
|
|
114
|
-
* @param result the result to be resolved for the Promise, or a function that supplies the
|
|
114
|
+
* @param result the result to be resolved for the Promise, or a function that supplies the result.
|
|
115
115
|
* @returns the new Promise created
|
|
116
116
|
*/
|
|
117
117
|
static delayedResolve<T>(ms: number, result?: T | PromiseLike<T> | (() => (T | PromiseLike<T>))): Promise<T>;
|
|
@@ -124,34 +124,36 @@ export declare abstract class PromiseUtils {
|
|
|
124
124
|
*/
|
|
125
125
|
static delayedReject<T = never, R = any>(ms: number, reason: R | PromiseLike<R> | (() => R | PromiseLike<R>)): Promise<T>;
|
|
126
126
|
/**
|
|
127
|
-
*
|
|
128
|
-
* If timeout
|
|
129
|
-
* If timeout
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* @param
|
|
134
|
-
* @param
|
|
135
|
-
* @
|
|
127
|
+
* Applies a timeout to a Promise or a function that returns a Promise.
|
|
128
|
+
* If the timeout occurs, resolves to the specified result.
|
|
129
|
+
* If the timeout doesn't occur, the resolved result or rejection reason of the original Promise will be the outcome of the Promise returned from this function.
|
|
130
|
+
* If the 'result' parameter is a function and timeout doesn't occur, the function won't be called.
|
|
131
|
+
* The rejection of the 'operation' parameter is not handled by this function, you may want to handle it outside of this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously".
|
|
132
|
+
*
|
|
133
|
+
* @param operation The original Promise or a function that returns a Promise for which the timeout will be applied.
|
|
134
|
+
* @param ms The number of milliseconds for the timeout.
|
|
135
|
+
* @param result The result to be resolved with if the timeout occurs, or a function that supplies the result.
|
|
136
|
+
* @return A new Promise that resolves to the specified result if the timeout occurs.
|
|
136
137
|
*/
|
|
137
|
-
static timeoutResolve<T>(operation: Promise<T
|
|
138
|
+
static timeoutResolve<T>(operation: Promise<T> | (() => Promise<T>), ms: number, result?: T | PromiseLike<T> | (() => (T | PromiseLike<T>)) | undefined): Promise<T>;
|
|
138
139
|
/**
|
|
139
|
-
*
|
|
140
|
-
* If timeout
|
|
141
|
-
* If timeout
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
* @param
|
|
146
|
-
* @param
|
|
147
|
-
* @
|
|
140
|
+
* Applies a timeout to a Promise or a function that returns a Promise.
|
|
141
|
+
* If the timeout occurs, rejects with the specified reason.
|
|
142
|
+
* If the timeout doesn't occur, the resolved result or rejection reason of the original Promise will be the outcome of the Promise returned from this function.
|
|
143
|
+
* If the 'reason' parameter is a function and timeout doesn't occur, the function won't be called.
|
|
144
|
+
* The rejection of the 'operation' parameter is not handled by this function, you may want to handle it outside of this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously".
|
|
145
|
+
*
|
|
146
|
+
* @param operation The original Promise or a function that returns a Promise for which the timeout will be applied.
|
|
147
|
+
* @param ms The number of milliseconds for the timeout.
|
|
148
|
+
* @param rejectReason The reason to reject with if the timeout occurs, or a function that supplies the reason.
|
|
149
|
+
* @return A new Promise that rejects with the specified reason if the timeout occurs.
|
|
148
150
|
*/
|
|
149
|
-
static timeoutReject<T = never, R = any>(operation: Promise<T
|
|
151
|
+
static timeoutReject<T = never, R = any>(operation: Promise<T> | (() => Promise<T>), ms: number, rejectReason: R | PromiseLike<R> | (() => R | PromiseLike<R>)): Promise<T>;
|
|
150
152
|
/**
|
|
151
153
|
* Get the state of the Promise.
|
|
152
154
|
* Please note that the returned value is a Promise, although it resolves immediately.
|
|
153
155
|
* @param p the Promise for which we would like to know its state
|
|
154
|
-
* @return A Promise that resolves immediately
|
|
156
|
+
* @return A Promise that resolves immediately containing the state of the input Promise
|
|
155
157
|
*/
|
|
156
158
|
static promiseState(p: Promise<any>): Promise<PromiseState>;
|
|
157
159
|
private static synchronizationLocks;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-utils.d.ts","sourceRoot":"","sources":["../src/promise-utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,UAAkJ,CAAC;AAElL;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,UAA6K,CAAC;AAE/M;;GAEG;AACH,oBAAY,YAAY;IACtB,OAAO,YA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= "Fulfilled";
|
|
35
35
|
PromiseState["Rejected"] = "Rejected";
|
|
36
|
-
})(PromiseState
|
|
36
|
+
})(PromiseState || (exports.PromiseState = PromiseState = {}));
|
|
37
37
|
class PromiseUtils {
|
|
38
38
|
/**
|
|
39
39
|
* Do an operation repeatedly and collect all the results.
|
|
@@ -42,7 +42,7 @@ class PromiseUtils {
|
|
|
42
42
|
* @example
|
|
43
43
|
* const domainNameObjects = await PromiseUtils.repeat(
|
|
44
44
|
* pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(),
|
|
45
|
-
*
|
|
45
|
+
* response => response.position? {position: response.position} : null,
|
|
46
46
|
* (collection, response) => collection.concat(response.items!),
|
|
47
47
|
* [] as APIGateway.DomainName[],
|
|
48
48
|
* );
|
|
@@ -167,7 +167,7 @@ class PromiseUtils {
|
|
|
167
167
|
/**
|
|
168
168
|
* Create a Promise that resolves after number of milliseconds specified
|
|
169
169
|
* @param ms number of milliseconds after which the created Promise would resolve
|
|
170
|
-
* @param result the result to be resolved for the Promise, or a function that supplies the
|
|
170
|
+
* @param result the result to be resolved for the Promise, or a function that supplies the result.
|
|
171
171
|
* @returns the new Promise created
|
|
172
172
|
*/
|
|
173
173
|
static delayedResolve(ms, result) {
|
|
@@ -189,40 +189,44 @@ class PromiseUtils {
|
|
|
189
189
|
}, ms));
|
|
190
190
|
}
|
|
191
191
|
/**
|
|
192
|
-
*
|
|
193
|
-
* If timeout
|
|
194
|
-
* If timeout
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* @param
|
|
199
|
-
* @param
|
|
200
|
-
* @
|
|
192
|
+
* Applies a timeout to a Promise or a function that returns a Promise.
|
|
193
|
+
* If the timeout occurs, resolves to the specified result.
|
|
194
|
+
* If the timeout doesn't occur, the resolved result or rejection reason of the original Promise will be the outcome of the Promise returned from this function.
|
|
195
|
+
* If the 'result' parameter is a function and timeout doesn't occur, the function won't be called.
|
|
196
|
+
* The rejection of the 'operation' parameter is not handled by this function, you may want to handle it outside of this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously".
|
|
197
|
+
*
|
|
198
|
+
* @param operation The original Promise or a function that returns a Promise for which the timeout will be applied.
|
|
199
|
+
* @param ms The number of milliseconds for the timeout.
|
|
200
|
+
* @param result The result to be resolved with if the timeout occurs, or a function that supplies the result.
|
|
201
|
+
* @return A new Promise that resolves to the specified result if the timeout occurs.
|
|
201
202
|
*/
|
|
202
203
|
static timeoutResolve(operation, ms, result) {
|
|
204
|
+
const promise = typeof operation === 'function' ? operation() : operation;
|
|
203
205
|
return Promise.race([
|
|
204
|
-
|
|
205
|
-
PromiseUtils.delayedResolve(ms, () => PromiseUtils.promiseState(
|
|
206
|
+
promise,
|
|
207
|
+
PromiseUtils.delayedResolve(ms, () => PromiseUtils.promiseState(promise)
|
|
206
208
|
.then(state => state === PromiseState.Pending ?
|
|
207
209
|
(typeof result === 'function' ? result() : result) :
|
|
208
210
|
{})),
|
|
209
211
|
]);
|
|
210
212
|
}
|
|
211
213
|
/**
|
|
212
|
-
*
|
|
213
|
-
* If timeout
|
|
214
|
-
* If timeout
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* @param
|
|
219
|
-
* @param
|
|
220
|
-
* @
|
|
214
|
+
* Applies a timeout to a Promise or a function that returns a Promise.
|
|
215
|
+
* If the timeout occurs, rejects with the specified reason.
|
|
216
|
+
* If the timeout doesn't occur, the resolved result or rejection reason of the original Promise will be the outcome of the Promise returned from this function.
|
|
217
|
+
* If the 'reason' parameter is a function and timeout doesn't occur, the function won't be called.
|
|
218
|
+
* The rejection of the 'operation' parameter is not handled by this function, you may want to handle it outside of this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously".
|
|
219
|
+
*
|
|
220
|
+
* @param operation The original Promise or a function that returns a Promise for which the timeout will be applied.
|
|
221
|
+
* @param ms The number of milliseconds for the timeout.
|
|
222
|
+
* @param rejectReason The reason to reject with if the timeout occurs, or a function that supplies the reason.
|
|
223
|
+
* @return A new Promise that rejects with the specified reason if the timeout occurs.
|
|
221
224
|
*/
|
|
222
225
|
static timeoutReject(operation, ms, rejectReason) {
|
|
226
|
+
const promise = typeof operation === 'function' ? operation() : operation;
|
|
223
227
|
return Promise.race([
|
|
224
|
-
|
|
225
|
-
PromiseUtils.delayedReject(ms, () => PromiseUtils.promiseState(
|
|
228
|
+
promise,
|
|
229
|
+
PromiseUtils.delayedReject(ms, () => PromiseUtils.promiseState(promise)
|
|
226
230
|
.then(state => state === PromiseState.Pending ?
|
|
227
231
|
(typeof rejectReason === 'function' ? rejectReason() : rejectReason) :
|
|
228
232
|
{})),
|
|
@@ -232,7 +236,7 @@ class PromiseUtils {
|
|
|
232
236
|
* Get the state of the Promise.
|
|
233
237
|
* Please note that the returned value is a Promise, although it resolves immediately.
|
|
234
238
|
* @param p the Promise for which we would like to know its state
|
|
235
|
-
* @return A Promise that resolves immediately
|
|
239
|
+
* @return A Promise that resolves immediately containing the state of the input Promise
|
|
236
240
|
*/
|
|
237
241
|
static promiseState(p) {
|
|
238
242
|
const t = {};
|
|
@@ -257,16 +261,19 @@ class PromiseUtils {
|
|
|
257
261
|
previousState = await PromiseUtils.promiseState(previousResultPromise);
|
|
258
262
|
}
|
|
259
263
|
switch (previousState) {
|
|
260
|
-
case PromiseState.Pending: // concurrency
|
|
264
|
+
case PromiseState.Pending: { // concurrency
|
|
261
265
|
resultPromise = previousResultPromise.then(result => operation(PromiseState.Pending, PromiseState.Fulfilled, result), error => operation(PromiseState.Pending, PromiseState.Rejected, error));
|
|
262
266
|
break;
|
|
263
|
-
|
|
267
|
+
}
|
|
268
|
+
case undefined: { // no concurrency and no history
|
|
264
269
|
// eslint-disable-next-line unicorn/no-useless-undefined
|
|
265
270
|
resultPromise = operation(undefined, undefined, undefined);
|
|
266
271
|
break;
|
|
267
|
-
|
|
272
|
+
}
|
|
273
|
+
default: { // no concurrency but with history
|
|
268
274
|
resultPromise = operation(previousState, previousState, await previousResultPromise.catch(error => error));
|
|
269
275
|
break;
|
|
276
|
+
}
|
|
270
277
|
}
|
|
271
278
|
PromiseUtils.synchronizationLocks.set(lock, resultPromise);
|
|
272
279
|
return resultPromise;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@handy-common-utils/promise-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Promise related utilities",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"pretest": "eslint . --ext .ts",
|
|
7
7
|
"test": "nyc mocha",
|
|
8
8
|
"prepare": "shx rm -rf dist && tsc && es-check",
|
|
9
|
-
"preversion": "generate-api-docs-and-update-readme &&
|
|
9
|
+
"preversion": "generate-api-docs-and-update-readme && git add README.md"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"package.json",
|
|
@@ -16,11 +16,8 @@
|
|
|
16
16
|
"types": "dist/promise-utils.d.ts",
|
|
17
17
|
"bin": {},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@handy-common-utils/dev-dependencies": "^1.
|
|
20
|
-
"@types/
|
|
21
|
-
"chai-as-promised": "^7.1.1",
|
|
22
|
-
"es-check": "^5.2.3",
|
|
23
|
-
"eslint": "^7.32.0"
|
|
19
|
+
"@handy-common-utils/dev-dependencies-mocha": "^1.3.1",
|
|
20
|
+
"@types/node": "^18.17.1"
|
|
24
21
|
},
|
|
25
22
|
"publishConfig": {
|
|
26
23
|
"access": "public"
|
|
@@ -34,10 +31,18 @@
|
|
|
34
31
|
"url": "https://github.com/handy-common-utils/promise-utils/issues"
|
|
35
32
|
},
|
|
36
33
|
"keywords": [
|
|
37
|
-
"
|
|
34
|
+
"promise",
|
|
38
35
|
"utils",
|
|
36
|
+
"retry",
|
|
37
|
+
"delay",
|
|
38
|
+
"resolve",
|
|
39
|
+
"reject",
|
|
39
40
|
"utilities"
|
|
40
41
|
],
|
|
41
42
|
"author": "James Hu",
|
|
42
|
-
"license": "Apache-2.0"
|
|
43
|
+
"license": "Apache-2.0",
|
|
44
|
+
"volta": {
|
|
45
|
+
"node": "18.17.0",
|
|
46
|
+
"npm": "9.6.7"
|
|
47
|
+
}
|
|
43
48
|
}
|