@handy-common-utils/promise-utils 1.3.0 → 1.4.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 +24 -10
- package/dist/promise-utils.d.ts +22 -9
- package/dist/promise-utils.d.ts.map +1 -1
- package/dist/promise-utils.js +21 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -181,7 +181,7 @@ ___
|
|
|
181
181
|
|
|
182
182
|
#### inParallel
|
|
183
183
|
|
|
184
|
-
▸ **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`): `Promise`<(`Result` \| `TError`)[]\>
|
|
184
|
+
▸ **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`, `options?`): `Promise`<(`Result` \| `TError`)[]\>
|
|
185
185
|
|
|
186
186
|
See [inParallel](#inparallel) for full documentation.
|
|
187
187
|
|
|
@@ -200,6 +200,8 @@ See [inParallel](#inparallel) for full documentation.
|
|
|
200
200
|
| `parallelism` | `number` |
|
|
201
201
|
| `jobs` | `Iterable`<`Data`\> |
|
|
202
202
|
| `operation` | (`job`: `Data`, `index`: `number`) => `Promise`<`Result`\> |
|
|
203
|
+
| `options?` | `Object` |
|
|
204
|
+
| `options.abortOnError` | `boolean` |
|
|
203
205
|
|
|
204
206
|
##### Returns
|
|
205
207
|
|
|
@@ -455,10 +457,17 @@ ___
|
|
|
455
457
|
|
|
456
458
|
##### inParallel
|
|
457
459
|
|
|
458
|
-
▸ `Static` **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`): `Promise`<(`Result` \| `TError`)[]\>
|
|
460
|
+
▸ `Static` **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`, `options?`): `Promise`<(`Result` \| `TError`)[]\>
|
|
459
461
|
|
|
460
462
|
Run multiple jobs/operations in parallel.
|
|
461
463
|
|
|
464
|
+
By default this function does not throw / reject with error when any of the job/operation fails.
|
|
465
|
+
Operation errors are returned together with operation results in the same returned array.
|
|
466
|
+
That also means this function only returns when all the jobs/operations settle (either resolve or reject).
|
|
467
|
+
|
|
468
|
+
However, if options.abortOnError is true, this function throws / rejects with error when any of the job/operation fails.
|
|
469
|
+
That also means, some of the jobs/operations may not get the chance to be executed if one of them fails.
|
|
470
|
+
|
|
462
471
|
###### Type parameters
|
|
463
472
|
|
|
464
473
|
| Name | Type | Description |
|
|
@@ -474,26 +483,31 @@ Run multiple jobs/operations in parallel.
|
|
|
474
483
|
| `parallelism` | `number` | how many jobs/operations can be running at the same time |
|
|
475
484
|
| `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. |
|
|
476
485
|
| `operation` | (`job`: `Data`, `index`: `number`) => `Promise`<`Result`\> | the function that turns job data into result asynchronously |
|
|
486
|
+
| `options?` | `Object` | Options for controlling the behavior of this function. |
|
|
487
|
+
| `options.abortOnError` | `boolean` | - |
|
|
477
488
|
|
|
478
489
|
###### Returns
|
|
479
490
|
|
|
480
491
|
`Promise`<(`Result` \| `TError`)[]\>
|
|
481
492
|
|
|
482
493
|
Promise of void if the operation function does not return a value,
|
|
483
|
-
or promise of an array containing
|
|
484
|
-
In the array containing
|
|
494
|
+
or promise of an array containing outcomes from the operation function.
|
|
495
|
+
In the returned array containing outcomes, each element is either the fulfilled result, or the rejected error/reason.
|
|
485
496
|
|
|
486
497
|
**`Example`**
|
|
487
498
|
|
|
488
499
|
```ts
|
|
489
|
-
const
|
|
490
|
-
await PromiseUtils.inParallel(5, topicArns, async topicArn => {
|
|
500
|
+
const attributesAndPossibleErrors = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => {
|
|
491
501
|
const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
|
|
492
|
-
|
|
493
|
-
if (this.shouldInclude(topicArn)) {
|
|
494
|
-
inventory.snsTopicsByArn.set(topicArn, topicDetails);
|
|
495
|
-
}
|
|
502
|
+
return topicAttributes;
|
|
496
503
|
});
|
|
504
|
+
|
|
505
|
+
let results: Array<JobResult>;
|
|
506
|
+
try {
|
|
507
|
+
results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true });
|
|
508
|
+
} catch (error) {
|
|
509
|
+
// handle the error
|
|
510
|
+
}
|
|
497
511
|
```
|
|
498
512
|
|
|
499
513
|
___
|
package/dist/promise-utils.d.ts
CHANGED
|
@@ -86,16 +86,26 @@ export declare abstract class PromiseUtils {
|
|
|
86
86
|
/**
|
|
87
87
|
* Run multiple jobs/operations in parallel.
|
|
88
88
|
*
|
|
89
|
+
* By default this function does not throw / reject with error when any of the job/operation fails.
|
|
90
|
+
* Operation errors are returned together with operation results in the same returned array.
|
|
91
|
+
* That also means this function only returns when all the jobs/operations settle (either resolve or reject).
|
|
92
|
+
*
|
|
93
|
+
* However, if options.abortOnError is true, this function throws / rejects with error when any of the job/operation fails.
|
|
94
|
+
* That also means, some of the jobs/operations may not get the chance to be executed if one of them fails.
|
|
95
|
+
*
|
|
89
96
|
* @example
|
|
90
|
-
* const
|
|
91
|
-
* await PromiseUtils.inParallel(5, topicArns, async topicArn => {
|
|
97
|
+
* const attributesAndPossibleErrors = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => {
|
|
92
98
|
* const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
|
|
93
|
-
*
|
|
94
|
-
* if (this.shouldInclude(topicArn)) {
|
|
95
|
-
* inventory.snsTopicsByArn.set(topicArn, topicDetails);
|
|
96
|
-
* }
|
|
99
|
+
* return topicAttributes;
|
|
97
100
|
* });
|
|
98
101
|
*
|
|
102
|
+
* let results: Array<JobResult>;
|
|
103
|
+
* try {
|
|
104
|
+
* results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true });
|
|
105
|
+
* } catch (error) {
|
|
106
|
+
* // handle the error
|
|
107
|
+
* }
|
|
108
|
+
*
|
|
99
109
|
* @template Data Type of the job data, usually it would be an Array
|
|
100
110
|
* @template Result Type of the return value of the operation function
|
|
101
111
|
*
|
|
@@ -103,11 +113,14 @@ export declare abstract class PromiseUtils {
|
|
|
103
113
|
* @param jobs job data which will be the input to operation function.
|
|
104
114
|
* This function is safe when there are infinite unknown number of elements in the job data.
|
|
105
115
|
* @param operation the function that turns job data into result asynchronously
|
|
116
|
+
* @param options Options for controlling the behavior of this function.
|
|
106
117
|
* @returns Promise of void if the operation function does not return a value,
|
|
107
|
-
* or promise of an array containing
|
|
108
|
-
* In the array containing
|
|
118
|
+
* or promise of an array containing outcomes from the operation function.
|
|
119
|
+
* In the returned array containing outcomes, each element is either the fulfilled result, or the rejected error/reason.
|
|
109
120
|
*/
|
|
110
|
-
static inParallel<Data, Result, TError = Result>(parallelism: number, jobs: Iterable<Data>, operation: (job: Data, index: number) => Promise<Result
|
|
121
|
+
static inParallel<Data, Result, TError = Result>(parallelism: number, jobs: Iterable<Data>, operation: (job: Data, index: number) => Promise<Result>, options?: {
|
|
122
|
+
abortOnError: boolean;
|
|
123
|
+
}): Promise<Array<Result | TError>>;
|
|
111
124
|
/**
|
|
112
125
|
* Create a Promise that resolves after number of milliseconds specified
|
|
113
126
|
* @param ms number of milliseconds after which the created Promise would resolve
|
|
@@ -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,YAAY;IACnB,SAAS,cAAc;IACvB,QAAQ,aAAa;CACtB;AAED,8BAAsB,YAAY;IAChC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WACU,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,UAAU,EAC3C,SAAS,EAAE,CAAC,SAAS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Aa,mCAA6B,CAAC;AACxD;;GAEG;AACH,eAAO,MAAM,YAAY,kCAA4B,CAAC;AACtD;;GAEG;AACH,eAAO,MAAM,YAAY,kCAA4B,CAAC;AACtD;;GAEG;AACH,eAAO,MAAM,YAAY,kCAA4B,CAAC"}
|
package/dist/promise-utils.js
CHANGED
|
@@ -120,16 +120,26 @@ class PromiseUtils {
|
|
|
120
120
|
/**
|
|
121
121
|
* Run multiple jobs/operations in parallel.
|
|
122
122
|
*
|
|
123
|
+
* By default this function does not throw / reject with error when any of the job/operation fails.
|
|
124
|
+
* Operation errors are returned together with operation results in the same returned array.
|
|
125
|
+
* That also means this function only returns when all the jobs/operations settle (either resolve or reject).
|
|
126
|
+
*
|
|
127
|
+
* However, if options.abortOnError is true, this function throws / rejects with error when any of the job/operation fails.
|
|
128
|
+
* That also means, some of the jobs/operations may not get the chance to be executed if one of them fails.
|
|
129
|
+
*
|
|
123
130
|
* @example
|
|
124
|
-
* const
|
|
125
|
-
* await PromiseUtils.inParallel(5, topicArns, async topicArn => {
|
|
131
|
+
* const attributesAndPossibleErrors = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => {
|
|
126
132
|
* const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
|
|
127
|
-
*
|
|
128
|
-
* if (this.shouldInclude(topicArn)) {
|
|
129
|
-
* inventory.snsTopicsByArn.set(topicArn, topicDetails);
|
|
130
|
-
* }
|
|
133
|
+
* return topicAttributes;
|
|
131
134
|
* });
|
|
132
135
|
*
|
|
136
|
+
* let results: Array<JobResult>;
|
|
137
|
+
* try {
|
|
138
|
+
* results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true });
|
|
139
|
+
* } catch (error) {
|
|
140
|
+
* // handle the error
|
|
141
|
+
* }
|
|
142
|
+
*
|
|
133
143
|
* @template Data Type of the job data, usually it would be an Array
|
|
134
144
|
* @template Result Type of the return value of the operation function
|
|
135
145
|
*
|
|
@@ -137,11 +147,12 @@ class PromiseUtils {
|
|
|
137
147
|
* @param jobs job data which will be the input to operation function.
|
|
138
148
|
* This function is safe when there are infinite unknown number of elements in the job data.
|
|
139
149
|
* @param operation the function that turns job data into result asynchronously
|
|
150
|
+
* @param options Options for controlling the behavior of this function.
|
|
140
151
|
* @returns Promise of void if the operation function does not return a value,
|
|
141
|
-
* or promise of an array containing
|
|
142
|
-
* In the array containing
|
|
152
|
+
* or promise of an array containing outcomes from the operation function.
|
|
153
|
+
* In the returned array containing outcomes, each element is either the fulfilled result, or the rejected error/reason.
|
|
143
154
|
*/
|
|
144
|
-
static async inParallel(parallelism, jobs, operation) {
|
|
155
|
+
static async inParallel(parallelism, jobs, operation, options) {
|
|
145
156
|
if (parallelism < 1) {
|
|
146
157
|
parallelism = 1;
|
|
147
158
|
}
|
|
@@ -158,7 +169,7 @@ class PromiseUtils {
|
|
|
158
169
|
const job = iteratorResult.value;
|
|
159
170
|
const jobIndex = index++;
|
|
160
171
|
const jobResultPromise = operation(job, jobIndex);
|
|
161
|
-
jobResults[jobIndex] = await jobResultPromise.catch(error => error);
|
|
172
|
+
jobResults[jobIndex] = (options === null || options === void 0 ? void 0 : options.abortOnError) ? await jobResultPromise : await jobResultPromise.catch(error => error);
|
|
162
173
|
}
|
|
163
174
|
});
|
|
164
175
|
await Promise.all(promises);
|