@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 +1 -1
- package/src/promise.js +2 -2
- package/src/promise.test.js +9 -0
package/package.json
CHANGED
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 = []
|
package/src/promise.test.js
CHANGED
|
@@ -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)
|