shelving 1.126.3 → 1.127.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/package.json +1 -1
- package/util/async.d.ts +15 -0
- package/util/async.js +8 -0
package/package.json
CHANGED
package/util/async.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ImmutableArray } from "./array.js";
|
|
1
2
|
import type { ValueCallback } from "./callback.js";
|
|
2
3
|
import type { Report } from "./error.js";
|
|
3
4
|
/** Is a value an asynchronous value implementing a `then()` function. */
|
|
@@ -18,6 +19,20 @@ export declare function assertAsync<T>(value: PromiseLike<T> | T): asserts value
|
|
|
18
19
|
export declare function assertPromise<T>(value: Promise<T> | T): asserts value is Promise<T>;
|
|
19
20
|
/** Run any queued microtasks now. */
|
|
20
21
|
export declare function runMicrotasks(): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Run multiple promises concurrently.
|
|
24
|
+
*
|
|
25
|
+
* DH: An issue with `Promise.all()` is: if _one_ of its promises rejects, the parent promise rejects immediately.
|
|
26
|
+
* - This leaves all of the other promises lost in unhandled purgatory.
|
|
27
|
+
* - The program may then have dangling open threads that prevent the program from exiting, even after it has returned its main result.
|
|
28
|
+
* - This function waits for the resolution of *all* promises before rejecting.
|
|
29
|
+
*
|
|
30
|
+
* @param promises Promises that we need to wait for.
|
|
31
|
+
* @throws unknown Rethrows the first error found after resolving all the promises (the first in the _list_, not the first overall).
|
|
32
|
+
*/
|
|
33
|
+
export declare function runConcurrent<T extends ImmutableArray<unknown>>(...promises: T): Promise<{
|
|
34
|
+
readonly [P in keyof T]: Awaited<T[P]>;
|
|
35
|
+
}>;
|
|
21
36
|
/** Type of `Promise` with `._resolve()` and `._reject()` methods available. */
|
|
22
37
|
export declare abstract class AbstractPromise<T> extends Promise<T> {
|
|
23
38
|
static get [Symbol.species](): PromiseConstructor;
|
package/util/async.js
CHANGED
|
@@ -37,6 +37,14 @@ export function runMicrotasks() {
|
|
|
37
37
|
// Timeouts are part of the main event queue, and events in the main queue are run _after_ all microtasks complete.
|
|
38
38
|
return new Promise(resolve => setTimeout(resolve));
|
|
39
39
|
}
|
|
40
|
+
export async function runConcurrent(...promises) {
|
|
41
|
+
return (await Promise.allSettled(promises)).map(_getFulfilledResult);
|
|
42
|
+
}
|
|
43
|
+
function _getFulfilledResult(result) {
|
|
44
|
+
if (result.status === "rejected")
|
|
45
|
+
throw result.reason;
|
|
46
|
+
return result.value;
|
|
47
|
+
}
|
|
40
48
|
/** Type of `Promise` with `._resolve()` and `._reject()` methods available. */
|
|
41
49
|
export class AbstractPromise extends Promise {
|
|
42
50
|
// Make `this.then()` create a `Promise` not a `Deferred`
|