@tim-code/my-util 0.5.11 → 0.5.12

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.11",
3
+ "version": "0.5.12",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/promise.js CHANGED
@@ -72,12 +72,12 @@ export async function sleep(ms) {
72
72
  * @param {boolean=} $1.flatten Flattens values before returning; useful if promises return arrays
73
73
  * @param {boolean=} $1.abort If true, will return early if there are errors.
74
74
  * If false (default), will process all elements in the array (like Promise.allSettled()).
75
- * @param {Function} callback
75
+ * @param {Function} callback Default is identity function to enable passing promises as "array".
76
76
  * @returns {Object} {results, values, returned, errors}
77
77
  */
78
78
  export async function allSettled(
79
79
  { array, limit, limiter, flatten = false, abort = false },
80
- callback
80
+ callback = (promise) => promise
81
81
  ) {
82
82
  const results = []
83
83
  let returned = []
@@ -132,6 +132,15 @@ describe("allSettled", () => {
132
132
  expect(result.results.every((r) => r.status === "fulfilled")).toBe(true)
133
133
  })
134
134
 
135
+ it("returns correct structure for all fulfilled", async () => {
136
+ const arr = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]
137
+ const result = await allSettled({ array: arr })
138
+ expect(result.values).toEqual([1, 2, 3])
139
+ expect(result.returned).toEqual([1, 2, 3])
140
+ expect(result.errors).toEqual([])
141
+ expect(result.results.every((r) => r.status === "fulfilled")).toBe(true)
142
+ })
143
+
135
144
  it("handles rejected promises and collects errors", async () => {
136
145
  const arr = [1, 2, 3]
137
146
  const cb = (x) => (x === 2 ? Promise.reject("fail") : x + 1)