@handy-common-utils/promise-utils 1.1.2 → 1.2.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 +156 -398
- package/dist/promise-utils.d.ts +6 -3
- package/dist/promise-utils.d.ts.map +1 -1
- package/dist/promise-utils.js +22 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -45,338 +45,147 @@ import { repeat } from '@handy-common-utils/promise-utils';
|
|
|
45
45
|
<!-- API start -->
|
|
46
46
|
<a name="readmemd"></a>
|
|
47
47
|
|
|
48
|
-
@handy-common-utils/promise-utils
|
|
48
|
+
**[@handy-common-utils/promise-utils](#readmemd)**
|
|
49
|
+
|
|
50
|
+
> Globals
|
|
49
51
|
|
|
50
52
|
## @handy-common-utils/promise-utils
|
|
51
53
|
|
|
52
|
-
###
|
|
54
|
+
### Index
|
|
53
55
|
|
|
54
56
|
#### Enumerations
|
|
55
57
|
|
|
56
|
-
|
|
58
|
+
* [PromiseState](#enumspromisestatemd)
|
|
57
59
|
|
|
58
60
|
#### Classes
|
|
59
61
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
#### Variables
|
|
63
|
-
|
|
64
|
-
- [FIBONACCI\_SEQUENCE](#fibonacci_sequence)
|
|
62
|
+
* [PromiseUtils](#classespromiseutilsmd)
|
|
65
63
|
|
|
66
|
-
####
|
|
67
|
-
|
|
68
|
-
- [delayedReject](#delayedreject)
|
|
69
|
-
- [delayedResolve](#delayedresolve)
|
|
70
|
-
- [inParallel](#inparallel)
|
|
71
|
-
- [promiseState](#promisestate)
|
|
72
|
-
- [repeat](#repeat)
|
|
73
|
-
- [synchronised](#synchronised)
|
|
74
|
-
- [synchronized](#synchronized)
|
|
75
|
-
- [timeoutReject](#timeoutreject)
|
|
76
|
-
- [timeoutResolve](#timeoutresolve)
|
|
77
|
-
- [withRetry](#withretry)
|
|
78
|
-
|
|
79
|
-
### Variables
|
|
64
|
+
#### Type aliases
|
|
80
65
|
|
|
81
|
-
|
|
66
|
+
* [InParrellelResult](#inparrellelresult)
|
|
82
67
|
|
|
83
|
-
|
|
68
|
+
#### Variables
|
|
84
69
|
|
|
85
|
-
|
|
70
|
+
* [delayedReject](#delayedreject)
|
|
71
|
+
* [delayedResolve](#delayedresolve)
|
|
72
|
+
* [inParallel](#inparallel)
|
|
73
|
+
* [promiseState](#promisestate)
|
|
74
|
+
* [repeat](#repeat)
|
|
75
|
+
* [synchronized](#synchronized)
|
|
76
|
+
* [timeoutReject](#timeoutreject)
|
|
77
|
+
* [timeoutResolve](#timeoutresolve)
|
|
86
78
|
|
|
87
|
-
|
|
79
|
+
### Type aliases
|
|
88
80
|
|
|
89
|
-
|
|
81
|
+
#### InParrellelResult
|
|
90
82
|
|
|
91
|
-
|
|
83
|
+
Ƭ **InParrellelResult**\<T>: T *extends* void ? void : Array\<T>
|
|
92
84
|
|
|
93
|
-
|
|
94
|
-
| :------ | :------ |
|
|
95
|
-
| `T` | `never` |
|
|
96
|
-
| `R` | `any` |
|
|
85
|
+
##### Type parameters:
|
|
97
86
|
|
|
98
|
-
|
|
87
|
+
Name |
|
|
88
|
+
------ |
|
|
89
|
+
`T` |
|
|
99
90
|
|
|
100
|
-
|
|
101
|
-
| :------ | :------ |
|
|
102
|
-
| `ms` | `number` |
|
|
103
|
-
| `reason` | `R` \| () => `R` |
|
|
91
|
+
### Variables
|
|
104
92
|
|
|
105
|
-
|
|
93
|
+
#### delayedReject
|
|
106
94
|
|
|
107
|
-
`
|
|
95
|
+
• `Const` **delayedReject**: [delayedReject](#delayedreject) = PromiseUtils.delayedReject
|
|
108
96
|
|
|
109
97
|
___
|
|
110
98
|
|
|
111
99
|
#### delayedResolve
|
|
112
100
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
##### Type parameters
|
|
116
|
-
|
|
117
|
-
| Name |
|
|
118
|
-
| :------ |
|
|
119
|
-
| `T` |
|
|
120
|
-
|
|
121
|
-
##### Parameters
|
|
122
|
-
|
|
123
|
-
| Name | Type |
|
|
124
|
-
| :------ | :------ |
|
|
125
|
-
| `ms` | `number` |
|
|
126
|
-
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> |
|
|
127
|
-
|
|
128
|
-
##### Returns
|
|
129
|
-
|
|
130
|
-
`Promise`<`T`\>
|
|
101
|
+
• `Const` **delayedResolve**: [delayedResolve](#delayedresolve) = PromiseUtils.delayedResolve
|
|
131
102
|
|
|
132
103
|
___
|
|
133
104
|
|
|
134
105
|
#### inParallel
|
|
135
106
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
##### Type parameters
|
|
139
|
-
|
|
140
|
-
| Name | Type |
|
|
141
|
-
| :------ | :------ |
|
|
142
|
-
| `Data` | `Data` |
|
|
143
|
-
| `Result` | `Result` |
|
|
144
|
-
| `TError` | `Result` |
|
|
145
|
-
|
|
146
|
-
##### Parameters
|
|
147
|
-
|
|
148
|
-
| Name | Type |
|
|
149
|
-
| :------ | :------ |
|
|
150
|
-
| `parallelism` | `number` |
|
|
151
|
-
| `jobs` | `Iterable`<`Data`\> |
|
|
152
|
-
| `operation` | (`job`: `Data`, `index`: `number`) => `Promise`<`Result`\> |
|
|
153
|
-
|
|
154
|
-
##### Returns
|
|
155
|
-
|
|
156
|
-
`Promise`<(`Result` \| `TError`)[]\>
|
|
107
|
+
• `Const` **inParallel**: [inParallel](#inparallel) = PromiseUtils.inParallel
|
|
157
108
|
|
|
158
109
|
___
|
|
159
110
|
|
|
160
111
|
#### promiseState
|
|
161
112
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
##### Parameters
|
|
165
|
-
|
|
166
|
-
| Name | Type |
|
|
167
|
-
| :------ | :------ |
|
|
168
|
-
| `p` | `Promise`<`any`\> |
|
|
169
|
-
|
|
170
|
-
##### Returns
|
|
171
|
-
|
|
172
|
-
`Promise`<[`PromiseState`](#enumspromisestatemd)\>
|
|
113
|
+
• `Const` **promiseState**: [promiseState](#promisestate) = PromiseUtils.promiseState
|
|
173
114
|
|
|
174
115
|
___
|
|
175
116
|
|
|
176
117
|
#### repeat
|
|
177
118
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
##### Type parameters
|
|
181
|
-
|
|
182
|
-
| Name |
|
|
183
|
-
| :------ |
|
|
184
|
-
| `Result` |
|
|
185
|
-
| `Param` |
|
|
186
|
-
| `Collection` |
|
|
187
|
-
|
|
188
|
-
##### Parameters
|
|
189
|
-
|
|
190
|
-
| Name | Type |
|
|
191
|
-
| :------ | :------ |
|
|
192
|
-
| `operation` | (`parameter`: `Partial`<`Param`\>) => `Promise`<`Result`\> |
|
|
193
|
-
| `nextParameter` | (`response`: `Result`) => ``null`` \| `Partial`<`Param`\> \| `Promise`<`Partial`<`Param`\>\> |
|
|
194
|
-
| `collect` | (`collection`: `Collection`, `result`: `Result`) => `Collection` |
|
|
195
|
-
| `initialCollection` | `Collection` |
|
|
196
|
-
| `initialParameter` | `Partial`<`Param`\> |
|
|
197
|
-
|
|
198
|
-
##### Returns
|
|
199
|
-
|
|
200
|
-
`Promise`<`Collection`\>
|
|
201
|
-
|
|
202
|
-
___
|
|
203
|
-
|
|
204
|
-
#### synchronised
|
|
205
|
-
|
|
206
|
-
▸ `Const` **synchronised**<`T`\>(`lock`, `operation`): `Promise`<`T`\>
|
|
207
|
-
|
|
208
|
-
##### Type parameters
|
|
209
|
-
|
|
210
|
-
| Name |
|
|
211
|
-
| :------ |
|
|
212
|
-
| `T` |
|
|
213
|
-
|
|
214
|
-
##### Parameters
|
|
215
|
-
|
|
216
|
-
| Name | Type |
|
|
217
|
-
| :------ | :------ |
|
|
218
|
-
| `lock` | `unknown` |
|
|
219
|
-
| `operation` | (`previousState`: `undefined` \| [`PromiseState`](#enumspromisestatemd), `previousSettledState`: `undefined` \| [`PromiseState`](#enumspromisestatemd), `previousResult`: `any`) => `Promise`<`T`\> |
|
|
220
|
-
|
|
221
|
-
##### Returns
|
|
222
|
-
|
|
223
|
-
`Promise`<`T`\>
|
|
119
|
+
• `Const` **repeat**: [repeat](#repeat) = PromiseUtils.repeat
|
|
224
120
|
|
|
225
121
|
___
|
|
226
122
|
|
|
227
123
|
#### synchronized
|
|
228
124
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
##### Type parameters
|
|
232
|
-
|
|
233
|
-
| Name |
|
|
234
|
-
| :------ |
|
|
235
|
-
| `T` |
|
|
236
|
-
|
|
237
|
-
##### Parameters
|
|
238
|
-
|
|
239
|
-
| Name | Type |
|
|
240
|
-
| :------ | :------ |
|
|
241
|
-
| `lock` | `unknown` |
|
|
242
|
-
| `operation` | (`previousState`: `undefined` \| [`PromiseState`](#enumspromisestatemd), `previousSettledState`: `undefined` \| [`PromiseState`](#enumspromisestatemd), `previousResult`: `any`) => `Promise`<`T`\> |
|
|
243
|
-
|
|
244
|
-
##### Returns
|
|
245
|
-
|
|
246
|
-
`Promise`<`T`\>
|
|
125
|
+
• `Const` **synchronized**: [synchronized](#synchronized) = PromiseUtils.synchronized
|
|
247
126
|
|
|
248
127
|
___
|
|
249
128
|
|
|
250
129
|
#### timeoutReject
|
|
251
130
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
##### Type parameters
|
|
255
|
-
|
|
256
|
-
| Name | Type |
|
|
257
|
-
| :------ | :------ |
|
|
258
|
-
| `T` | `never` |
|
|
259
|
-
| `R` | `any` |
|
|
260
|
-
|
|
261
|
-
##### Parameters
|
|
262
|
-
|
|
263
|
-
| Name | Type |
|
|
264
|
-
| :------ | :------ |
|
|
265
|
-
| `operation` | `Promise`<`T`\> |
|
|
266
|
-
| `ms` | `number` |
|
|
267
|
-
| `rejectReason` | `R` \| () => `R` |
|
|
268
|
-
|
|
269
|
-
##### Returns
|
|
270
|
-
|
|
271
|
-
`Promise`<`T`\>
|
|
131
|
+
• `Const` **timeoutReject**: [timeoutReject](#timeoutreject) = PromiseUtils.timeoutReject
|
|
272
132
|
|
|
273
133
|
___
|
|
274
134
|
|
|
275
135
|
#### timeoutResolve
|
|
276
136
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
##### Type parameters
|
|
280
|
-
|
|
281
|
-
| Name |
|
|
282
|
-
| :------ |
|
|
283
|
-
| `T` |
|
|
284
|
-
|
|
285
|
-
##### Parameters
|
|
286
|
-
|
|
287
|
-
| Name | Type |
|
|
288
|
-
| :------ | :------ |
|
|
289
|
-
| `operation` | `Promise`<`T`\> |
|
|
290
|
-
| `ms` | `number` |
|
|
291
|
-
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> |
|
|
292
|
-
|
|
293
|
-
##### Returns
|
|
294
|
-
|
|
295
|
-
`Promise`<`T`\>
|
|
296
|
-
|
|
297
|
-
___
|
|
298
|
-
|
|
299
|
-
#### withRetry
|
|
300
|
-
|
|
301
|
-
▸ `Const` **withRetry**<`Result`, `TError`\>(`operation`, `backoff`, `shouldRetry?`): `Promise`<`Result`\>
|
|
302
|
-
|
|
303
|
-
##### Type parameters
|
|
304
|
-
|
|
305
|
-
| Name | Type |
|
|
306
|
-
| :------ | :------ |
|
|
307
|
-
| `Result` | `Result` |
|
|
308
|
-
| `TError` | `any` |
|
|
309
|
-
|
|
310
|
-
##### Parameters
|
|
311
|
-
|
|
312
|
-
| Name | Type |
|
|
313
|
-
| :------ | :------ |
|
|
314
|
-
| `operation` | (`attempt`: `number`, `previousResult`: `undefined` \| `Result`, `previousError`: `undefined` \| `TError`) => `Promise`<`Result`\> |
|
|
315
|
-
| `backoff` | `number`[] \| (`attempt`: `number`, `previousResult`: `undefined` \| `Result`, `previousError`: `undefined` \| `TError`) => `undefined` \| `number` |
|
|
316
|
-
| `shouldRetry` | (`previousError`: `undefined` \| `TError`, `previousResult`: `undefined` \| `Result`, `attempt`: `number`) => `boolean` |
|
|
317
|
-
|
|
318
|
-
##### Returns
|
|
319
|
-
|
|
320
|
-
`Promise`<`Result`\>
|
|
137
|
+
• `Const` **timeoutResolve**: [timeoutResolve](#timeoutresolve) = PromiseUtils.timeoutResolve
|
|
321
138
|
|
|
322
139
|
## Classes
|
|
323
140
|
|
|
324
141
|
|
|
325
142
|
<a name="classespromiseutilsmd"></a>
|
|
326
143
|
|
|
327
|
-
[@handy-common-utils/promise-utils](#readmemd)
|
|
144
|
+
**[@handy-common-utils/promise-utils](#readmemd)**
|
|
145
|
+
|
|
146
|
+
> [Globals](#readmemd) / PromiseUtils
|
|
328
147
|
|
|
329
148
|
### Class: PromiseUtils
|
|
330
149
|
|
|
331
|
-
####
|
|
150
|
+
#### Hierarchy
|
|
332
151
|
|
|
333
|
-
|
|
152
|
+
* **PromiseUtils**
|
|
334
153
|
|
|
335
|
-
|
|
154
|
+
#### Index
|
|
336
155
|
|
|
337
156
|
##### Methods
|
|
338
157
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
- [withRetry](#withretry)
|
|
348
|
-
|
|
349
|
-
#### Constructors
|
|
350
|
-
|
|
351
|
-
##### constructor
|
|
352
|
-
|
|
353
|
-
• **new PromiseUtils**()
|
|
158
|
+
* [delayedReject](#delayedreject)
|
|
159
|
+
* [delayedResolve](#delayedresolve)
|
|
160
|
+
* [inParallel](#inparallel)
|
|
161
|
+
* [promiseState](#promisestate)
|
|
162
|
+
* [repeat](#repeat)
|
|
163
|
+
* [synchronized](#synchronized)
|
|
164
|
+
* [timeoutReject](#timeoutreject)
|
|
165
|
+
* [timeoutResolve](#timeoutresolve)
|
|
354
166
|
|
|
355
167
|
#### Methods
|
|
356
168
|
|
|
357
169
|
##### delayedReject
|
|
358
170
|
|
|
359
|
-
▸ `Static` **delayedReject
|
|
171
|
+
▸ `Static` **delayedReject**\<T>(`ms`: number, `reason`: any): Promise\<T>
|
|
360
172
|
|
|
361
173
|
Create a Promise that rejects after number of milliseconds specified
|
|
362
174
|
|
|
363
|
-
###### Type parameters
|
|
175
|
+
###### Type parameters:
|
|
364
176
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
| `R` | `any` |
|
|
177
|
+
Name | Default |
|
|
178
|
+
------ | ------ |
|
|
179
|
+
`T` | never |
|
|
369
180
|
|
|
370
|
-
###### Parameters
|
|
181
|
+
###### Parameters:
|
|
371
182
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
183
|
+
Name | Type | Description |
|
|
184
|
+
------ | ------ | ------ |
|
|
185
|
+
`ms` | number | number of milliseconds after which the created Promise would reject |
|
|
186
|
+
`reason` | any | the reason of the rejection for the Promise |
|
|
376
187
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
`Promise`<`T`\>
|
|
188
|
+
**Returns:** Promise\<T>
|
|
380
189
|
|
|
381
190
|
the new Promise created
|
|
382
191
|
|
|
@@ -384,26 +193,24 @@ ___
|
|
|
384
193
|
|
|
385
194
|
##### delayedResolve
|
|
386
195
|
|
|
387
|
-
▸ `Static` **delayedResolve
|
|
196
|
+
▸ `Static` **delayedResolve**\<T>(`ms`: number, `result?`: T \| PromiseLike\<T> \| undefined): Promise\<T>
|
|
388
197
|
|
|
389
198
|
Create a Promise that resolves after number of milliseconds specified
|
|
390
199
|
|
|
391
|
-
###### Type parameters
|
|
392
|
-
|
|
393
|
-
| Name |
|
|
394
|
-
| :------ |
|
|
395
|
-
| `T` |
|
|
200
|
+
###### Type parameters:
|
|
396
201
|
|
|
397
|
-
|
|
202
|
+
Name |
|
|
203
|
+
------ |
|
|
204
|
+
`T` |
|
|
398
205
|
|
|
399
|
-
|
|
400
|
-
| :------ | :------ | :------ |
|
|
401
|
-
| `ms` | `number` | number of milliseconds after which the created Promise would resolve |
|
|
402
|
-
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> | the result to be resolved for the Promise, or a function that supplies the reuslt. |
|
|
206
|
+
###### Parameters:
|
|
403
207
|
|
|
404
|
-
|
|
208
|
+
Name | Type | Description |
|
|
209
|
+
------ | ------ | ------ |
|
|
210
|
+
`ms` | number | number of milliseconds after which the created Promise would resolve |
|
|
211
|
+
`result?` | T \| PromiseLike\<T> \| undefined | the result to be resolved for the Promise |
|
|
405
212
|
|
|
406
|
-
|
|
213
|
+
**Returns:** Promise\<T>
|
|
407
214
|
|
|
408
215
|
the new Promise created
|
|
409
216
|
|
|
@@ -411,15 +218,15 @@ ___
|
|
|
411
218
|
|
|
412
219
|
##### inParallel
|
|
413
220
|
|
|
414
|
-
▸ `Static` **inParallel
|
|
221
|
+
▸ `Static` **inParallel**\<Data, Result>(`parallelism`: number, `jobs`: Iterable\<Data>, `operation`: (job: Data, index: number) => Promise\<Result>): Promise\<[InParrellelResult](#inparrellelresult)\<Result>>
|
|
415
222
|
|
|
416
223
|
Run multiple jobs/operations in parallel.
|
|
417
224
|
|
|
418
225
|
**`example`**
|
|
419
226
|
```javascript
|
|
420
|
-
|
|
227
|
+
|
|
421
228
|
const topicArns = topics.map(topic => topic.TopicArn!);
|
|
422
|
-
await
|
|
229
|
+
await Utils.inParallel(5, topicArns, async topicArn => {
|
|
423
230
|
const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
|
|
424
231
|
const topicDetails = { ...topicAttributes, subscriptions: [] } as any;
|
|
425
232
|
if (this.shouldInclude(topicArn)) {
|
|
@@ -428,48 +235,42 @@ await PromiseUtils.inParallel(5, topicArns, async topicArn => {
|
|
|
428
235
|
});
|
|
429
236
|
|
|
430
237
|
```
|
|
431
|
-
###### Type parameters
|
|
238
|
+
###### Type parameters:
|
|
432
239
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
| `TError` | `Result` | - |
|
|
240
|
+
Name | Description |
|
|
241
|
+
------ | ------ |
|
|
242
|
+
`Data` | Type of the job data, usually it would be an Array |
|
|
243
|
+
`Result` | Type of the return value of the operation function |
|
|
438
244
|
|
|
439
|
-
###### Parameters
|
|
245
|
+
###### Parameters:
|
|
440
246
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
247
|
+
Name | Type | Description |
|
|
248
|
+
------ | ------ | ------ |
|
|
249
|
+
`parallelism` | number | how many jobs/operations can be running at the same time |
|
|
250
|
+
`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. |
|
|
251
|
+
`operation` | (job: Data, index: number) => Promise\<Result> | the function that turns job data into result asynchronously |
|
|
446
252
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
`Promise`<(`Result` \| `TError`)[]\>
|
|
253
|
+
**Returns:** Promise\<[InParrellelResult](#inparrellelresult)\<Result>>
|
|
450
254
|
|
|
451
255
|
Promise of void if the operation function does not return a value,
|
|
452
|
-
or promise of an
|
|
453
|
-
In the array containing results, each element is either the fulfilled result, or the rejected error/reason.
|
|
256
|
+
or promise of an arry containing results returned from the operation function.
|
|
454
257
|
|
|
455
258
|
___
|
|
456
259
|
|
|
457
260
|
##### promiseState
|
|
458
261
|
|
|
459
|
-
▸ `Static` **promiseState**(`p
|
|
262
|
+
▸ `Static` **promiseState**(`p`: Promise\<any>): Promise\<[PromiseState](#enumspromisestatemd)>
|
|
460
263
|
|
|
461
264
|
Get the state of the Promise.
|
|
462
265
|
Please note that the returned value is a Promise, although it resolves immediately.
|
|
463
266
|
|
|
464
|
-
###### Parameters
|
|
465
|
-
|
|
466
|
-
| Name | Type | Description |
|
|
467
|
-
| :------ | :------ | :------ |
|
|
468
|
-
| `p` | `Promise`<`any`\> | the Promise for which we would like to know its state |
|
|
267
|
+
###### Parameters:
|
|
469
268
|
|
|
470
|
-
|
|
269
|
+
Name | Type | Description |
|
|
270
|
+
------ | ------ | ------ |
|
|
271
|
+
`p` | Promise\<any> | the Promise for which we would like to know its state |
|
|
471
272
|
|
|
472
|
-
|
|
273
|
+
**Returns:** Promise\<[PromiseState](#enumspromisestatemd)>
|
|
473
274
|
|
|
474
275
|
A Promise that resolves immediately cotaining the state of the input Promise
|
|
475
276
|
|
|
@@ -477,15 +278,15 @@ ___
|
|
|
477
278
|
|
|
478
279
|
##### repeat
|
|
479
280
|
|
|
480
|
-
▸ `Static` **repeat
|
|
281
|
+
▸ `Static` **repeat**\<Result, Param, Collection>(`operation`: (parameter: Partial\<Param>) => Promise\<Result>, `nextParameter`: (response: Result) => Partial\<Param> \| null, `collect`: (collection: Collection, result: Result) => Collection, `initialCollection`: Collection, `initialParameter?`: Partial\<Param>): Promise\<Collection>
|
|
481
282
|
|
|
482
283
|
Do an operation repeatedly and collect all the results.
|
|
483
284
|
This function is useful for client side pagination.
|
|
484
285
|
|
|
485
286
|
**`example`**
|
|
486
287
|
```javascript
|
|
487
|
-
|
|
488
|
-
const domainNameObjects = await
|
|
288
|
+
|
|
289
|
+
const domainNameObjects = await Utils.repeat(
|
|
489
290
|
pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(),
|
|
490
291
|
esponse => response.position? {position: response.position} : null,
|
|
491
292
|
(collection, response) => collection.concat(response.items!),
|
|
@@ -493,27 +294,25 @@ const domainNameObjects = await PromiseUtils.repeat(
|
|
|
493
294
|
);
|
|
494
295
|
|
|
495
296
|
```
|
|
496
|
-
###### Type parameters
|
|
497
|
-
|
|
498
|
-
| Name | Description |
|
|
499
|
-
| :------ | :------ |
|
|
500
|
-
| `Result` | type of the operation result |
|
|
501
|
-
| `Param` | type of the input to the operation, normally the input is a paging parameter |
|
|
502
|
-
| `Collection` | type of the returned value of this function |
|
|
297
|
+
###### Type parameters:
|
|
503
298
|
|
|
504
|
-
|
|
299
|
+
Name | Description |
|
|
300
|
+
------ | ------ |
|
|
301
|
+
`Result` | type of the operation result |
|
|
302
|
+
`Param` | type of the input to the operation, normally the input is a paging parameter |
|
|
303
|
+
`Collection` | type of the returned value of this function |
|
|
505
304
|
|
|
506
|
-
|
|
507
|
-
| :------ | :------ | :------ |
|
|
508
|
-
| `operation` | (`parameter`: `Partial`<`Param`\>) => `Promise`<`Result`\> | a function that takes paging parameter as input and outputs a result, normally the operation supports paging |
|
|
509
|
-
| `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. |
|
|
510
|
-
| `collect` | (`collection`: `Collection`, `result`: `Result`) => `Collection` | the function for merging operation result into the collection |
|
|
511
|
-
| `initialCollection` | `Collection` | initial collection which would be the first argument passed into the first invocation of the collect function |
|
|
512
|
-
| `initialParameter` | `Partial`<`Param`\> | the parameter for the first operation |
|
|
305
|
+
###### Parameters:
|
|
513
306
|
|
|
514
|
-
|
|
307
|
+
Name | Type | Default value | Description |
|
|
308
|
+
------ | ------ | ------ | ------ |
|
|
309
|
+
`operation` | (parameter: Partial\<Param>) => Promise\<Result> | - | a function that takes paging parameter as input and outputs a result, normally the operation supports paging |
|
|
310
|
+
`nextParameter` | (response: Result) => Partial\<Param> \| null | - | 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. |
|
|
311
|
+
`collect` | (collection: Collection, result: Result) => Collection | - | the function for merging operation result into the collection |
|
|
312
|
+
`initialCollection` | Collection | - | initial collection which would be the first argument passed into the first invocation of the collect function |
|
|
313
|
+
`initialParameter` | Partial\<Param> | {} | the parameter for the first operation |
|
|
515
314
|
|
|
516
|
-
|
|
315
|
+
**Returns:** Promise\<Collection>
|
|
517
316
|
|
|
518
317
|
Promise of collection of all the results returned by the operation function
|
|
519
318
|
|
|
@@ -521,30 +320,27 @@ ___
|
|
|
521
320
|
|
|
522
321
|
##### synchronized
|
|
523
322
|
|
|
524
|
-
▸ `Static` **synchronized
|
|
323
|
+
▸ `Static` **synchronized**\<T>(`lock`: any, `operation`: (previousState: [PromiseState](#enumspromisestatemd) \| undefined, previousSettledState: [PromiseState](#enumspromisestatemd) \| undefined, previousResult: any) => Promise\<T>): Promise\<T>
|
|
525
324
|
|
|
526
325
|
Equivalent of `synchronized` in Java.
|
|
527
326
|
In any situation there's no concurrent execution of any operation function associated with the same lock.
|
|
528
|
-
The operation function has access to the state (when `synchronized` is called), settledState (when the operation function is called),
|
|
529
|
-
and result (could be the fulfilled result or the rejected reason) of the previous operation.
|
|
327
|
+
The operation function has access to the state (when `synchronized` is called), settledState (when the operation function is called), and result of the previous operation.
|
|
530
328
|
In case there is no previous invocation, state, settledState and result would all be undefined.
|
|
531
329
|
|
|
532
|
-
###### Type parameters
|
|
330
|
+
###### Type parameters:
|
|
533
331
|
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
332
|
+
Name |
|
|
333
|
+
------ |
|
|
334
|
+
`T` |
|
|
537
335
|
|
|
538
|
-
###### Parameters
|
|
336
|
+
###### Parameters:
|
|
539
337
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
338
|
+
Name | Type | Description |
|
|
339
|
+
------ | ------ | ------ |
|
|
340
|
+
`lock` | any | the object (could be a string, a number, or `this` in a class) that is used to apply the lock |
|
|
341
|
+
`operation` | (previousState: [PromiseState](#enumspromisestatemd) \| undefined, previousSettledState: [PromiseState](#enumspromisestatemd) \| undefined, previousResult: any) => Promise\<T> | function for doing the computation and returning a Promise |
|
|
544
342
|
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
`Promise`<`T`\>
|
|
343
|
+
**Returns:** Promise\<T>
|
|
548
344
|
|
|
549
345
|
the result of the operation function
|
|
550
346
|
|
|
@@ -552,29 +348,26 @@ ___
|
|
|
552
348
|
|
|
553
349
|
##### timeoutReject
|
|
554
350
|
|
|
555
|
-
▸ `Static` **timeoutReject
|
|
351
|
+
▸ `Static` **timeoutReject**\<T>(`operation`: Promise\<T>, `ms`: number, `rejectReason`: any): Promise\<T>
|
|
556
352
|
|
|
557
353
|
Apply timeout to an operation, in case timeout happens, reject with the reason specified.
|
|
558
354
|
If timeout does not happen, the resolved result or rejection reason of the original operation would be returned.
|
|
559
355
|
|
|
560
|
-
###### Type parameters
|
|
561
|
-
|
|
562
|
-
| Name | Type |
|
|
563
|
-
| :------ | :------ |
|
|
564
|
-
| `T` | `never` |
|
|
565
|
-
| `R` | `any` |
|
|
356
|
+
###### Type parameters:
|
|
566
357
|
|
|
567
|
-
|
|
358
|
+
Name |
|
|
359
|
+
------ |
|
|
360
|
+
`T` |
|
|
568
361
|
|
|
569
|
-
|
|
570
|
-
| :------ | :------ | :------ |
|
|
571
|
-
| `operation` | `Promise`<`T`\> | the original operation that timeout would be applied |
|
|
572
|
-
| `ms` | `number` | number of milliseconds for the timeout |
|
|
573
|
-
| `rejectReason` | `R` \| () => `R` | the reason of the rejection in case timeout happens, or a function that supplies the reason. |
|
|
362
|
+
###### Parameters:
|
|
574
363
|
|
|
575
|
-
|
|
364
|
+
Name | Type | Description |
|
|
365
|
+
------ | ------ | ------ |
|
|
366
|
+
`operation` | Promise\<T> | the original operation that timeout would be applied |
|
|
367
|
+
`ms` | number | number of milliseconds for the timeout |
|
|
368
|
+
`rejectReason` | any | the reason of the rejection in case timeout happens |
|
|
576
369
|
|
|
577
|
-
|
|
370
|
+
**Returns:** Promise\<T>
|
|
578
371
|
|
|
579
372
|
the new Promise that rejects with the specified reason in case timeout happens
|
|
580
373
|
|
|
@@ -582,98 +375,63 @@ ___
|
|
|
582
375
|
|
|
583
376
|
##### timeoutResolve
|
|
584
377
|
|
|
585
|
-
▸ `Static` **timeoutResolve
|
|
378
|
+
▸ `Static` **timeoutResolve**\<T>(`operation`: Promise\<T>, `ms`: number, `result?`: T \| PromiseLike\<T> \| undefined): Promise\<T>
|
|
586
379
|
|
|
587
380
|
Apply timeout to an operation, in case timeout happens, resolve to the result specified.
|
|
588
381
|
If timeout does not happen, the resolved result or rejection reason of the original operation would be returned.
|
|
589
382
|
|
|
590
|
-
###### Type parameters
|
|
591
|
-
|
|
592
|
-
| Name |
|
|
593
|
-
| :------ |
|
|
594
|
-
| `T` |
|
|
383
|
+
###### Type parameters:
|
|
595
384
|
|
|
596
|
-
|
|
385
|
+
Name |
|
|
386
|
+
------ |
|
|
387
|
+
`T` |
|
|
597
388
|
|
|
598
|
-
|
|
599
|
-
| :------ | :------ | :------ |
|
|
600
|
-
| `operation` | `Promise`<`T`\> | the original operation that timeout would be applied |
|
|
601
|
-
| `ms` | `number` | number of milliseconds for the timeout |
|
|
602
|
-
| `result?` | `T` \| `PromiseLike`<`T`\> \| () => `T` \| `PromiseLike`<`T`\> | the result to be resolved in case timeout happens, or a function that supplies the reuslt. |
|
|
389
|
+
###### Parameters:
|
|
603
390
|
|
|
604
|
-
|
|
391
|
+
Name | Type | Description |
|
|
392
|
+
------ | ------ | ------ |
|
|
393
|
+
`operation` | Promise\<T> | the original operation that timeout would be applied |
|
|
394
|
+
`ms` | number | number of milliseconds for the timeout |
|
|
395
|
+
`result?` | T \| PromiseLike\<T> \| undefined | the result to be resolved in case timeout happens |
|
|
605
396
|
|
|
606
|
-
|
|
397
|
+
**Returns:** Promise\<T>
|
|
607
398
|
|
|
608
399
|
the new Promise that resolves to the specified result in case timeout happens
|
|
609
400
|
|
|
610
|
-
___
|
|
611
|
-
|
|
612
|
-
##### withRetry
|
|
613
|
-
|
|
614
|
-
▸ `Static` **withRetry**<`Result`, `TError`\>(`operation`, `backoff`, `shouldRetry?`): `Promise`<`Result`\>
|
|
615
|
-
|
|
616
|
-
Do an operation repeatedly until a criteria is met.
|
|
617
|
-
|
|
618
|
-
**`example`**
|
|
619
|
-
```javascript
|
|
620
|
-
|
|
621
|
-
const result = await PromiseUtils.withRetry(() => doSomething(), [100, 200, 300, 500, 800, 1000]);
|
|
622
|
-
|
|
623
|
-
```
|
|
624
|
-
###### Type parameters
|
|
625
|
-
|
|
626
|
-
| Name | Type | Description |
|
|
627
|
-
| :------ | :------ | :------ |
|
|
628
|
-
| `Result` | `Result` | type of the operation result |
|
|
629
|
-
| `TError` | `any` | type of the possible error that could be generated by the operation |
|
|
630
|
-
|
|
631
|
-
###### Parameters
|
|
632
|
-
|
|
633
|
-
| Name | Type | Description |
|
|
634
|
-
| :------ | :------ | :------ |
|
|
635
|
-
| `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 |
|
|
636
|
-
| `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 2 because only retries need to backoff, so the first retry is the second attempt. |
|
|
637
|
-
| `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. |
|
|
638
|
-
|
|
639
|
-
###### Returns
|
|
640
|
-
|
|
641
|
-
`Promise`<`Result`\>
|
|
642
|
-
|
|
643
|
-
Promise of the operation result potentially with retries already applied
|
|
644
|
-
|
|
645
401
|
## Enums
|
|
646
402
|
|
|
647
403
|
|
|
648
404
|
<a name="enumspromisestatemd"></a>
|
|
649
405
|
|
|
650
|
-
[@handy-common-utils/promise-utils](#readmemd)
|
|
406
|
+
**[@handy-common-utils/promise-utils](#readmemd)**
|
|
407
|
+
|
|
408
|
+
> [Globals](#readmemd) / PromiseState
|
|
651
409
|
|
|
652
410
|
### Enumeration: PromiseState
|
|
653
411
|
|
|
654
|
-
####
|
|
412
|
+
#### Index
|
|
655
413
|
|
|
656
414
|
##### Enumeration members
|
|
657
415
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
416
|
+
* [Fulfilled](#fulfilled)
|
|
417
|
+
* [Pending](#pending)
|
|
418
|
+
* [Rejected](#rejected)
|
|
661
419
|
|
|
662
420
|
#### Enumeration members
|
|
663
421
|
|
|
664
422
|
##### Fulfilled
|
|
665
423
|
|
|
666
|
-
•
|
|
424
|
+
• **Fulfilled**: = "Fulfilled"
|
|
667
425
|
|
|
668
426
|
___
|
|
669
427
|
|
|
670
428
|
##### Pending
|
|
671
429
|
|
|
672
|
-
•
|
|
430
|
+
• **Pending**: = "Pending"
|
|
673
431
|
|
|
674
432
|
___
|
|
675
433
|
|
|
676
434
|
##### Rejected
|
|
677
435
|
|
|
678
|
-
•
|
|
436
|
+
• **Rejected**: = "Rejected"
|
|
679
437
|
<!-- API end -->
|
package/dist/promise-utils.d.ts
CHANGED
|
@@ -89,15 +89,17 @@ export declare abstract class PromiseUtils {
|
|
|
89
89
|
*/
|
|
90
90
|
static delayedResolve<T>(ms: number, result?: T | PromiseLike<T> | (() => (T | PromiseLike<T>))): Promise<T>;
|
|
91
91
|
/**
|
|
92
|
-
* Create a Promise that rejects after number of milliseconds specified
|
|
92
|
+
* Create a Promise that rejects after number of milliseconds specified.
|
|
93
93
|
* @param ms number of milliseconds after which the created Promise would reject
|
|
94
94
|
* @param reason the reason of the rejection for the Promise, or a function that supplies the reason.
|
|
95
|
+
* If the reason ends up to be a rejected Promise, then the outcome (could be fulfilled or rejected) of it will be the reject reason of the Promise returned.
|
|
95
96
|
* @returns the new Promise created
|
|
96
97
|
*/
|
|
97
|
-
static delayedReject<T = never, R = any>(ms: number, reason: R | (() => R)): Promise<T>;
|
|
98
|
+
static delayedReject<T = never, R = any>(ms: number, reason: R | PromiseLike<R> | (() => R | PromiseLike<R>)): Promise<T>;
|
|
98
99
|
/**
|
|
99
100
|
* Apply timeout to an operation, in case timeout happens, resolve to the result specified.
|
|
100
101
|
* If timeout does not happen, the resolved result or rejection reason of the original operation would be returned.
|
|
102
|
+
* If timeout does not happen and result is a function, the function won't be called.
|
|
101
103
|
* @param operation the original operation that timeout would be applied
|
|
102
104
|
* @param ms number of milliseconds for the timeout
|
|
103
105
|
* @param result the result to be resolved in case timeout happens, or a function that supplies the reuslt.
|
|
@@ -107,12 +109,13 @@ export declare abstract class PromiseUtils {
|
|
|
107
109
|
/**
|
|
108
110
|
* Apply timeout to an operation, in case timeout happens, reject with the reason specified.
|
|
109
111
|
* If timeout does not happen, the resolved result or rejection reason of the original operation would be returned.
|
|
112
|
+
* If timeout does not happen and rejectReason is a function, the function won't be called.
|
|
110
113
|
* @param operation the original operation that timeout would be applied
|
|
111
114
|
* @param ms number of milliseconds for the timeout
|
|
112
115
|
* @param rejectReason the reason of the rejection in case timeout happens, or a function that supplies the reason.
|
|
113
116
|
* @return the new Promise that rejects with the specified reason in case timeout happens
|
|
114
117
|
*/
|
|
115
|
-
static timeoutReject<T = never, R = any>(operation: Promise<T>, ms: number, rejectReason: R | (() => R)): Promise<T>;
|
|
118
|
+
static timeoutReject<T = never, R = any>(operation: Promise<T>, ms: number, rejectReason: R | PromiseLike<R> | (() => R | PromiseLike<R>)): Promise<T>;
|
|
116
119
|
/**
|
|
117
120
|
* Get the state of the Promise.
|
|
118
121
|
* Please note that the returned value is a Promise, although it resolves immediately.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-utils.d.ts","sourceRoot":"","sources":["../src/promise-utils.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,kBAAkB,UAAkJ,CAAC;AAElL,oBAAY,YAAY;IACtB,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,QAAQ,aAAa;CACtB;AAED,8BAAsB,YAAY;IAChC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WAEU,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,UAAU,EAC3C,SAAS,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,EACzD,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,K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
|
|
|
151
151
|
return new Promise(resolve => setTimeout(() => resolve(typeof result === 'function' ? result() : result), ms));
|
|
152
152
|
}
|
|
153
153
|
/**
|
|
154
|
-
* Create a Promise that rejects after number of milliseconds specified
|
|
154
|
+
* Create a Promise that rejects after number of milliseconds specified.
|
|
155
155
|
* @param ms number of milliseconds after which the created Promise would reject
|
|
156
156
|
* @param reason the reason of the rejection for the Promise, or a function that supplies the reason.
|
|
157
|
+
* If the reason ends up to be a rejected Promise, then the outcome (could be fulfilled or rejected) of it will be the reject reason of the Promise returned.
|
|
157
158
|
* @returns the new Promise created
|
|
158
159
|
*/
|
|
159
160
|
static delayedReject(ms, reason) {
|
|
160
|
-
return new Promise((_resolve, reject) => setTimeout(() =>
|
|
161
|
+
return new Promise((_resolve, reject) => setTimeout(() => {
|
|
162
|
+
const r = typeof reason === 'function' ? reason() : reason;
|
|
163
|
+
Promise.resolve(r).catch(error => error).then(r => reject(r));
|
|
164
|
+
}, ms));
|
|
161
165
|
}
|
|
162
166
|
/**
|
|
163
167
|
* Apply timeout to an operation, in case timeout happens, resolve to the result specified.
|
|
164
168
|
* If timeout does not happen, the resolved result or rejection reason of the original operation would be returned.
|
|
169
|
+
* If timeout does not happen and result is a function, the function won't be called.
|
|
165
170
|
* @param operation the original operation that timeout would be applied
|
|
166
171
|
* @param ms number of milliseconds for the timeout
|
|
167
172
|
* @param result the result to be resolved in case timeout happens, or a function that supplies the reuslt.
|
|
168
173
|
* @return the new Promise that resolves to the specified result in case timeout happens
|
|
169
174
|
*/
|
|
170
175
|
static timeoutResolve(operation, ms, result) {
|
|
171
|
-
return Promise.race([
|
|
176
|
+
return Promise.race([
|
|
177
|
+
operation,
|
|
178
|
+
PromiseUtils.delayedResolve(ms, () => PromiseUtils.promiseState(operation)
|
|
179
|
+
.then(state => state === PromiseState.Pending ?
|
|
180
|
+
(typeof result === 'function' ? result() : result) :
|
|
181
|
+
{})),
|
|
182
|
+
]);
|
|
172
183
|
}
|
|
173
184
|
/**
|
|
174
185
|
* Apply timeout to an operation, in case timeout happens, reject with the reason specified.
|
|
175
186
|
* If timeout does not happen, the resolved result or rejection reason of the original operation would be returned.
|
|
187
|
+
* If timeout does not happen and rejectReason is a function, the function won't be called.
|
|
176
188
|
* @param operation the original operation that timeout would be applied
|
|
177
189
|
* @param ms number of milliseconds for the timeout
|
|
178
190
|
* @param rejectReason the reason of the rejection in case timeout happens, or a function that supplies the reason.
|
|
179
191
|
* @return the new Promise that rejects with the specified reason in case timeout happens
|
|
180
192
|
*/
|
|
181
193
|
static timeoutReject(operation, ms, rejectReason) {
|
|
182
|
-
return Promise.race([
|
|
194
|
+
return Promise.race([
|
|
195
|
+
operation,
|
|
196
|
+
PromiseUtils.delayedReject(ms, () => PromiseUtils.promiseState(operation)
|
|
197
|
+
.then(state => state === PromiseState.Pending ?
|
|
198
|
+
(typeof rejectReason === 'function' ? rejectReason() : rejectReason) :
|
|
199
|
+
{})),
|
|
200
|
+
]);
|
|
183
201
|
}
|
|
184
202
|
/**
|
|
185
203
|
* Get the state of the Promise.
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@handy-common-utils/promise-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Promise related utilities",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"pretest": "eslint . --ext .ts",
|
|
7
|
-
"test": "nyc mocha
|
|
7
|
+
"test": "nyc mocha",
|
|
8
8
|
"prepare": "shx rm -rf dist && tsc && es-check",
|
|
9
9
|
"preversion": "generate-api-docs-and-update-readme && git add README.md"
|
|
10
10
|
},
|