@handy-common-utils/promise-utils 1.3.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,15 +60,20 @@ const result3 = await withRetry(() => doSomething(), attempt => attempt <= 8 ? 1
60
60
  statusCode === 429);
61
61
 
62
62
  // inParallel(...)
63
- const topicArns = topics.map(topic => topic.TopicArn!);
64
- await inParallel(5, topicArns, async topicArn => {
63
+ // Capture errors in the returned array
64
+ const attributesAndPossibleErrors = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => {
65
65
  const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
66
- const topicDetails = { ...topicAttributes, subscriptions: [] } as any;
67
- if (this.shouldInclude(topicArn)) {
68
- inventory.snsTopicsByArn.set(topicArn, topicDetails);
69
- }
66
+ return topicAttributes;
70
67
  });
71
68
 
69
+ // Abort on the first error
70
+ let results: Array<JobResult>;
71
+ try {
72
+ results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true });
73
+ } catch (error) {
74
+ // handle the error
75
+ }
76
+
72
77
  ```
73
78
 
74
79
  # API
@@ -181,7 +186,7 @@ ___
181
186
 
182
187
  #### inParallel
183
188
 
184
- ▸ **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`): `Promise`<(`Result` \| `TError`)[]\>
189
+ ▸ **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`, `options?`): `Promise`<(`Result` \| `TError`)[]\>
185
190
 
186
191
  See [inParallel](#inparallel) for full documentation.
187
192
 
@@ -200,6 +205,8 @@ See [inParallel](#inparallel) for full documentation.
200
205
  | `parallelism` | `number` |
201
206
  | `jobs` | `Iterable`<`Data`\> |
202
207
  | `operation` | (`job`: `Data`, `index`: `number`) => `Promise`<`Result`\> |
208
+ | `options?` | `Object` |
209
+ | `options.abortOnError` | `boolean` |
203
210
 
204
211
  ##### Returns
205
212
 
@@ -455,10 +462,17 @@ ___
455
462
 
456
463
  ##### inParallel
457
464
 
458
- ▸ `Static` **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`): `Promise`<(`Result` \| `TError`)[]\>
465
+ ▸ `Static` **inParallel**<`Data`, `Result`, `TError`\>(`parallelism`, `jobs`, `operation`, `options?`): `Promise`<(`Result` \| `TError`)[]\>
459
466
 
460
467
  Run multiple jobs/operations in parallel.
461
468
 
469
+ By default this function does not throw / reject with error when any of the job/operation fails.
470
+ Operation errors are returned together with operation results in the same returned array.
471
+ That also means this function only returns when all the jobs/operations settle (either resolve or reject).
472
+
473
+ However, if options.abortOnError is true, this function throws / rejects with error when any of the job/operation fails.
474
+ That also means, some of the jobs/operations may not get the chance to be executed if one of them fails.
475
+
462
476
  ###### Type parameters
463
477
 
464
478
  | Name | Type | Description |
@@ -474,26 +488,33 @@ Run multiple jobs/operations in parallel.
474
488
  | `parallelism` | `number` | how many jobs/operations can be running at the same time |
475
489
  | `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
490
  | `operation` | (`job`: `Data`, `index`: `number`) => `Promise`<`Result`\> | the function that turns job data into result asynchronously |
491
+ | `options?` | `Object` | Options for controlling the behavior of this function. |
492
+ | `options.abortOnError` | `boolean` | - |
477
493
 
478
494
  ###### Returns
479
495
 
480
496
  `Promise`<(`Result` \| `TError`)[]\>
481
497
 
482
498
  Promise of void if the operation function does not return a value,
483
- or promise of an array containing results returned from the operation function.
484
- In the array containing results, each element is either the fulfilled result, or the rejected error/reason.
499
+ or promise of an array containing outcomes from the operation function.
500
+ In the returned array containing outcomes, each element is either the fulfilled result, or the rejected error/reason.
485
501
 
486
502
  **`Example`**
487
503
 
488
504
  ```ts
489
- const topicArns = topics.map(topic => topic.TopicArn!);
490
- await PromiseUtils.inParallel(5, topicArns, async topicArn => {
505
+ // Capture errors in the returned array
506
+ const attributesAndPossibleErrors = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => {
491
507
  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
- }
508
+ return topicAttributes;
496
509
  });
510
+
511
+ // Abort on the first error
512
+ let results: Array<JobResult>;
513
+ try {
514
+ results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true });
515
+ } catch (error) {
516
+ // handle the error
517
+ }
497
518
  ```
498
519
 
499
520
  ___
@@ -86,16 +86,28 @@ 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 topicArns = topics.map(topic => topic.TopicArn!);
91
- * await PromiseUtils.inParallel(5, topicArns, async topicArn => {
97
+ * // Capture errors in the returned array
98
+ * const attributesAndPossibleErrors = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => {
92
99
  * const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
93
- * const topicDetails = { ...topicAttributes, subscriptions: [] } as any;
94
- * if (this.shouldInclude(topicArn)) {
95
- * inventory.snsTopicsByArn.set(topicArn, topicDetails);
96
- * }
100
+ * return topicAttributes;
97
101
  * });
98
102
  *
103
+ * // Abort on the first error
104
+ * let results: Array<JobResult>;
105
+ * try {
106
+ * results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true });
107
+ * } catch (error) {
108
+ * // handle the error
109
+ * }
110
+ *
99
111
  * @template Data Type of the job data, usually it would be an Array
100
112
  * @template Result Type of the return value of the operation function
101
113
  *
@@ -103,11 +115,14 @@ export declare abstract class PromiseUtils {
103
115
  * @param jobs job data which will be the input to operation function.
104
116
  * This function is safe when there are infinite unknown number of elements in the job data.
105
117
  * @param operation the function that turns job data into result asynchronously
118
+ * @param options Options for controlling the behavior of this function.
106
119
  * @returns Promise of void if the operation function does not return a value,
107
- * or promise of an array containing results returned from the operation function.
108
- * In the array containing results, each element is either the fulfilled result, or the rejected error/reason.
120
+ * or promise of an array containing outcomes from the operation function.
121
+ * In the returned array containing outcomes, each element is either the fulfilled result, or the rejected error/reason.
109
122
  */
110
- static inParallel<Data, Result, TError = Result>(parallelism: number, jobs: Iterable<Data>, operation: (job: Data, index: number) => Promise<Result>): Promise<Array<Result | TError>>;
123
+ static inParallel<Data, Result, TError = Result>(parallelism: number, jobs: Iterable<Data>, operation: (job: Data, index: number) => Promise<Result>, options?: {
124
+ abortOnError: boolean;
125
+ }): Promise<Array<Result | TError>>;
111
126
  /**
112
127
  * Create a Promise that resolves after number of milliseconds specified
113
128
  * @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,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,EACzD,aAAa,EAAE,CA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C1D;;GAEG;AACH,eAAO,MAAM,aAAa,mCAA6B,CAAC;AACxD;;GAEG;AACH,eAAO,MAAM,cAAc,oCAA8B,CAAC;AAC1D;;GAEG;AACH,eAAO,MAAM,aAAa,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"}
@@ -120,16 +120,28 @@ 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 topicArns = topics.map(topic => topic.TopicArn!);
125
- * await PromiseUtils.inParallel(5, topicArns, async topicArn => {
131
+ * // Capture errors in the returned array
132
+ * const attributesAndPossibleErrors = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => {
126
133
  * const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!;
127
- * const topicDetails = { ...topicAttributes, subscriptions: [] } as any;
128
- * if (this.shouldInclude(topicArn)) {
129
- * inventory.snsTopicsByArn.set(topicArn, topicDetails);
130
- * }
134
+ * return topicAttributes;
131
135
  * });
132
136
  *
137
+ * // Abort on the first error
138
+ * let results: Array<JobResult>;
139
+ * try {
140
+ * results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true });
141
+ * } catch (error) {
142
+ * // handle the error
143
+ * }
144
+ *
133
145
  * @template Data Type of the job data, usually it would be an Array
134
146
  * @template Result Type of the return value of the operation function
135
147
  *
@@ -137,11 +149,12 @@ class PromiseUtils {
137
149
  * @param jobs job data which will be the input to operation function.
138
150
  * This function is safe when there are infinite unknown number of elements in the job data.
139
151
  * @param operation the function that turns job data into result asynchronously
152
+ * @param options Options for controlling the behavior of this function.
140
153
  * @returns Promise of void if the operation function does not return a value,
141
- * or promise of an array containing results returned from the operation function.
142
- * In the array containing results, each element is either the fulfilled result, or the rejected error/reason.
154
+ * or promise of an array containing outcomes from the operation function.
155
+ * In the returned array containing outcomes, each element is either the fulfilled result, or the rejected error/reason.
143
156
  */
144
- static async inParallel(parallelism, jobs, operation) {
157
+ static async inParallel(parallelism, jobs, operation, options) {
145
158
  if (parallelism < 1) {
146
159
  parallelism = 1;
147
160
  }
@@ -158,7 +171,7 @@ class PromiseUtils {
158
171
  const job = iteratorResult.value;
159
172
  const jobIndex = index++;
160
173
  const jobResultPromise = operation(job, jobIndex);
161
- jobResults[jobIndex] = await jobResultPromise.catch(error => error);
174
+ jobResults[jobIndex] = (options === null || options === void 0 ? void 0 : options.abortOnError) ? await jobResultPromise : await jobResultPromise.catch(error => error);
162
175
  }
163
176
  });
164
177
  await Promise.all(promises);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@handy-common-utils/promise-utils",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "Promise related utilities",
5
5
  "scripts": {
6
6
  "pretest": "eslint . --ext .ts",