@tim-code/my-util 0.5.14 → 0.5.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.5.14",
3
+ "version": "0.5.15",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/promise.js CHANGED
@@ -59,35 +59,36 @@ export async function sleep(ms) {
59
59
  * Parallelize executions of a function using `Promise.allSettled()`.
60
60
  * This is useful because usually you want to set a limit to the number of parallel requests possible at once.
61
61
  * The output of this function contains the outcome of each call of the callback in a variety of formats.
62
- * - "results": Essentially the return value from Promise.allSettled().
62
+ * - "results": An array with the same elements as returned by Promise.allSettled().
63
63
  * - "values": The "value" property of each "result" object returned from allSettled(); this will be undefined if the associated promise rejects.
64
64
  * - "returned": The "value" property for each "result" object where the "status" property has a value of fulfilled.
65
65
  * This is arguably more useful than "values" unless you need to be able to index into the array based on the index of the promise.
66
66
  * - "errors": The "reason" property for each "result" object that did not have a status of "fulfilled".
67
67
  * @param {Object} $1
68
- * @param {Array} $1.array
68
+ * @param {Iterable} $1.array
69
+ * @param {Iterable=} $1.iterable An alternative, more accurate property name for specifying "array"
69
70
  * @param {number=} $1.limit The number of calls to do in parallel. If not provided, each call is done in parallel.
70
71
  * @param {Function=} $1.limiter A function awaited after a group of parallel calls is processed.
71
72
  * It is called with the number of parallel calls processed. Could be as simple as `() => sleep(10000)` if you wanted to wait 10 seconds between.
72
- * @param {boolean=} $1.flatten Flattens values before returning; useful if promises return arrays
73
+ * @param {boolean=} $1.flatten Flattens "values" and "returned" before returning; useful if promises return arrays
73
74
  * @param {boolean=} $1.abort If true, will return early if there are errors.
74
75
  * If false (default), will process all elements in the array (like Promise.allSettled()).
75
76
  * @param {Function} callback Default is identity function to enable passing promises as "array".
76
77
  * @returns {Object} {results, values, returned, errors}
77
78
  */
78
79
  export async function allSettled(
79
- { array, limit, limiter, flatten = false, abort = false },
80
+ { array, iterable = array, limit, limiter, flatten = false, abort = false },
80
81
  callback = (promise) => promise
81
82
  ) {
82
83
  const results = []
83
84
  let returned = []
84
85
  let values = []
85
86
  const errors = []
86
- const chunked = chunk(array, limit)
87
+ const chunked = chunk(iterable, limit)
87
88
  for (const elements of chunked) {
88
89
  const promises = elements.map(callback)
89
- const _results = await Promise.allSettled(promises)
90
- for (const result of _results) {
90
+ const chunkResults = await Promise.allSettled(promises)
91
+ for (const result of chunkResults) {
91
92
  const { value, status, reason } = result
92
93
  results.push(result)
93
94
  values.push(value)
@@ -1,6 +1,11 @@
1
1
  /* eslint-disable prefer-promise-reject-errors */
2
2
  import { jest } from "@jest/globals"
3
3
 
4
+ // Exported API under test:
5
+ // - class PollError
6
+ // - functions: poll, sleep, allSettled, intervalLimiter, alert, throwFirstReject
7
+ // Code changes in this diff affect: allSettled (added "iterable" param and generalized to Iterable)
8
+
4
9
  import {
5
10
  alert,
6
11
  allSettled,
@@ -214,6 +219,20 @@ describe("allSettled", () => {
214
219
  expect(cb).toHaveBeenCalledTimes(2)
215
220
  // Should not process [3,4] or [5,6]
216
221
  })
222
+
223
+ it("accepts non-Array iterable via iterable option (Map)", async () => {
224
+ const map = new Map([
225
+ ["a", 1],
226
+ ["b", 2],
227
+ ["c", 3],
228
+ ])
229
+ const cb = ([k, v]) => `${k}:${v * 2}`
230
+ const result = await allSettled({ iterable: map, limit: 2 }, cb)
231
+ expect(result.values).toEqual(["a:2", "b:4", "c:6"])
232
+ expect(result.returned).toEqual(["a:2", "b:4", "c:6"])
233
+ expect(result.errors).toEqual([])
234
+ expect(result.results.every((r) => r.status === "fulfilled")).toBe(true)
235
+ })
217
236
  })
218
237
 
219
238
  describe("intervalLimiter", () => {