Skip to content

Commit 5bf64f2

Browse files
committed
Promise types refactoring for pure version
1 parent 5bfb957 commit 5bf64f2

File tree

12 files changed

+105
-56
lines changed

12 files changed

+105
-56
lines changed

packages/core-js-types/src/base/proposals/promise-all-settled.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ interface PromiseConstructor {
1313
* @param values - An array of Promises.
1414
* @returns A new Promise.
1515
*/
16-
allSettled<T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: CoreJS.CoreJSPromiseSettledResult<Awaited<T[P]>>; }>; // @type-options: prefix-return-type
16+
allSettled<T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: CoreJS.CoreJSPromiseSettledResult<Awaited<T[P]>>; }>;
1717

1818
/**
1919
* Creates a Promise that is resolved with an array of results when all
2020
* of the provided Promises resolve or reject.
2121
* @param values - An array of Promises.
2222
* @returns A new Promise.
2323
*/
24-
allSettled<T>(values: Iterable<T | PromiseLike<T>>): Promise<CoreJS.CoreJSPromiseSettledResult<Awaited<T>>[]>; // @type-options: prefix-return-type
24+
allSettled<T>(values: Iterable<T | PromiseLike<T>>): Promise<CoreJS.CoreJSPromiseSettledResult<Awaited<T>>[]>;
2525
}

packages/core-js-types/src/base/proposals/promise-any.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ interface PromiseConstructor {
1010
* @param values - An array or iterable of Promises.
1111
* @returns A new Promise.
1212
*/
13-
any<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>; // @type-options: prefix-return-type
13+
any<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>;
1414

1515
/**
1616
* The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
1717
* @param values - An array or iterable of Promises.
1818
* @returns A new Promise.
1919
*/
20-
any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>; // @type-options: prefix-return-type
20+
any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>;
2121
}
2222

23-
interface AggregateError extends Error { // @type-options: no-redefine
23+
interface AggregateError extends Error {
2424
errors: any[];
2525
}
2626

27-
interface AggregateErrorConstructor { // @type-options: no-extends, no-redefine
27+
interface AggregateErrorConstructor {
2828
new (errors: Iterable<any>, message?: string): AggregateError;
2929
(errors: Iterable<any>, message?: string): AggregateError;
3030
readonly prototype: AggregateError;

packages/core-js-types/src/base/proposals/promise-finally.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ interface Promise<T> {
1111
* @param onfinally - The callback to execute when the Promise is settled (fulfilled or rejected).
1212
* @returns A Promise for the completion of the callback.
1313
*/
14-
finally(onfinally?: (() => void) | undefined | null): Promise<T>; // @type-options: prefix-return-type
14+
finally(onfinally?: (() => void) | undefined | null): Promise<T>;
1515
}

packages/core-js-types/src/base/proposals/promise-try.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ interface PromiseConstructor {
1818
* - Already rejected, if the callback synchronously throws an error.
1919
* - Asynchronously fulfilled or rejected, if the callback returns a promise.
2020
*/
21-
try<T, U extends unknown[]>(callbackFn: (...args: U) => T | PromiseLike<T>, ...args: U): Promise<Awaited<T>>; // @type-options: prefix-return-type
21+
try<T, U extends unknown[]>(callbackFn: (...args: U) => T | PromiseLike<T>, ...args: U): Promise<Awaited<T>>;
2222
}
2323

packages/core-js-types/src/base/proposals/promise-with-resolvers.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// https://github.com/microsoft/TypeScript/blob/af3a3779de6bc27619c85077e1b4d1de8feddd35/src/lib/es2024.promise.d.ts
55
// License: https://github.com/microsoft/TypeScript/blob/v5.9.3/LICENSE.txt
66

7-
interface PromiseWithResolvers<T> { // @type-options: no-extends, no-prefix
8-
promise: Promise<T>; // @type-options: prefix-return-type
7+
interface PromiseWithResolvers<T> {
8+
promise: Promise<T>;
99

1010
resolve: (value: T | PromiseLike<T>) => void;
1111

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
declare namespace CoreJS {
2+
export interface CoreJSAggregateError extends Error {
3+
errors: any[];
4+
}
5+
6+
export interface CoreJSAggregateErrorConstructor {
7+
new (errors: Iterable<any>, message?: string): CoreJSAggregateError;
8+
(errors: Iterable<any>, message?: string): CoreJSAggregateError;
9+
readonly prototype: CoreJSAggregateError;
10+
}
11+
12+
var CoreJSAggregateError: CoreJSAggregateErrorConstructor;
13+
}

packages/core-js-types/src/base/pure/core-js-types/promise.d.ts

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
/// <reference types="./promise-settled-result" />
2+
3+
// https://github.com/tc39/proposal-await-dictionary
4+
5+
// https://github.com/tc39/proposal-promise-allSettled
6+
7+
// https://github.com/tc39/proposal-promise-any
8+
9+
// https://github.com/tc39/proposal-promise-try
10+
11+
// https://github.com/tc39/proposal-promise-with-resolvers
12+
113
// For ensuring compatibility with TypeScript standard types, this code is aligned with:
2-
// https://github.com/microsoft/TypeScript/blob/347254895823a36a1b1b1c80471422da54ad77de/src/lib/es2021.promise.d.ts#L16
3-
// https://github.com/microsoft/TypeScript/blob/347254895823a36a1b1b1c80471422da54ad77de/src/lib/es2025.promise.d.ts#L4
14+
// https://github.com/microsoft/TypeScript/blob/2a90a739c1c1e87e3c3d0c93e16f7e5baadf8035/src/lib/es2020.promise.d.ts
15+
// https://github.com/microsoft/TypeScript/blob/347254895823a36a1b1b1c80471422da54ad77de/src/lib/es2021.promise.d.ts
16+
// https://github.com/microsoft/TypeScript/blob/af3a3779de6bc27619c85077e1b4d1de8feddd35/src/lib/es2024.promise.d.ts
17+
// https://github.com/microsoft/TypeScript/blob/347254895823a36a1b1b1c80471422da54ad77de/src/lib/es2025.promise.d.ts
18+
// https://github.com/microsoft/TypeScript/blob/af3a3779de6bc27619c85077e1b4d1de8feddd35/src/lib/esnext.promise.d.ts
419
// License: https://github.com/microsoft/TypeScript/blob/v5.9.3/LICENSE.txt
520

621
declare namespace CoreJS {
@@ -31,6 +46,51 @@ declare namespace CoreJS {
3146
*/
3247
all<T extends readonly unknown[] | []>(values: T): CoreJSPromise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>;
3348

49+
/**
50+
* Takes an object of promises and returns a single Promise that resolves to an object
51+
* with the same keys and fulfilled values, or rejects as soon as any of the input promises rejects.
52+
* @param promises - An object of promises
53+
* @returns A new `Promise` that resolves to an object of fulfilled values or rejects if any promise rejects.
54+
*/
55+
allKeyed<D extends Record<PropertyKey, unknown>>(promises: D): CoreJSPromise<{ [k in keyof D]: Awaited<D[k]> }>;
56+
57+
/**
58+
* Takes an object whose values are promises and returns a single `Promise` that resolves
59+
* to an object with the same keys, after all of the input promises have settled.
60+
* @param promises - An object of promises
61+
* @returns A new Promise that resolves to an object with the same keys as the input object,
62+
* where each key maps to the settlement result (`{ status, value }` or `{ status, reason }`) of the corresponding promise.
63+
*/
64+
allSettledKeyed<D extends Record<PropertyKey, unknown>>(promises: D): CoreJSPromise<{ [k in keyof D]: CoreJS.CoreJSPromiseSettledResult<Awaited<D[k]>> }>;
65+
66+
/**
67+
* Creates a Promise that is resolved with an array of results when all
68+
* of the provided Promises resolve or reject.
69+
* @param values - An array of Promises.
70+
* @returns A new Promise.
71+
*/
72+
allSettled<T extends readonly unknown[] | []>(values: T): CoreJSPromise<{ -readonly [P in keyof T]: CoreJS.CoreJSPromiseSettledResult<Awaited<T[P]>>; }>;
73+
/**
74+
* Creates a Promise that is resolved with an array of results when all
75+
* of the provided Promises resolve or reject.
76+
* @param values - An array of Promises.
77+
* @returns A new Promise.
78+
*/
79+
allSettled<T>(values: Iterable<T | PromiseLike<T>>): CoreJSPromise<CoreJS.CoreJSPromiseSettledResult<Awaited<T>>[]>;
80+
81+
/**
82+
* The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
83+
* @param values - An array or iterable of Promises.
84+
* @returns A new Promise.
85+
*/
86+
any<T extends readonly unknown[] | []>(values: T): CoreJSPromise<Awaited<T[number]>>;
87+
/**
88+
* The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
89+
* @param values - An array or iterable of Promises.
90+
* @returns A new Promise.
91+
*/
92+
any<T>(values: Iterable<T | PromiseLike<T>>): CoreJSPromise<Awaited<T>>;
93+
3494
/**
3595
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
3696
* or rejected.
@@ -58,21 +118,6 @@ declare namespace CoreJS {
58118
*/
59119
resolve<T>(value: T | PromiseLike<T>): CoreJSPromise<Awaited<T>>;
60120

61-
// allSettled, any, try
62-
63-
/**
64-
* The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
65-
* @param values - An array or iterable of Promises.
66-
* @returns A new Promise.
67-
*/
68-
any<T extends readonly unknown[] | []>(values: T): CoreJSPromise<Awaited<T[number]>>;
69-
/**
70-
* The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
71-
* @param values - An array or iterable of Promises.
72-
* @returns A new Promise.
73-
*/
74-
any<T>(values: Iterable<T | PromiseLike<T>>): CoreJSPromise<Awaited<T>>;
75-
76121
/**
77122
* Takes a callback of any kind (returns or throws, synchronously or asynchronously) and wraps its result
78123
* in a Promise.
@@ -87,6 +132,16 @@ declare namespace CoreJS {
87132
* - Asynchronously fulfilled or rejected, if the callback returns a promise.
88133
*/
89134
try<T, U extends unknown[]>(callbackFn: (...args: U) => T | PromiseLike<T>, ...args: U): CoreJSPromise<Awaited<T>>;
135+
136+
/**
137+
* Creates a new Promise and returns it in an object, along with its resolve and reject functions.
138+
* @returns An object with the properties `promise`, `resolve`, and `reject`.
139+
*
140+
* ```ts
141+
* const { promise, resolve, reject } = Promise.withResolvers<T>();
142+
* ```
143+
*/
144+
withResolvers<T>(): CoreJSPromiseWithResolvers<T>;
90145
}
91146

92147
var CoreJSPromise: CoreJSPromiseConstructor;
Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,2 @@
1-
/// <reference types="../../core-js-types/promise-settled-result" />
2-
/// <reference types="../core-js-types/promise" />
3-
4-
// Motivation: Using our own Promise type in return
5-
6-
// https://github.com/tc39/proposal-await-dictionary
7-
8-
declare namespace CoreJS {
9-
export interface CoreJSPromiseConstructor extends PromiseConstructor {
10-
/**
11-
* Takes an object of promises and returns a single Promise that resolves to an object
12-
* with the same keys and fulfilled values, or rejects as soon as any of the input promises rejects.
13-
* @param promises - An object of promises
14-
* @returns A new `Promise` that resolves to an object of fulfilled values or rejects if any promise rejects.
15-
*/
16-
allKeyed<D extends Record<PropertyKey, unknown>>(promises: D): CoreJS.CoreJSPromise<{ [k in keyof D]: Awaited<D[k]> }>;
17-
18-
/**
19-
* Takes an object whose values are promises and returns a single `Promise` that resolves
20-
* to an object with the same keys, after all of the input promises have settled.
21-
* @param promises - An object of promises
22-
* @returns A new Promise that resolves to an object with the same keys as the input object,
23-
* where each key maps to the settlement result (`{ status, value }` or `{ status, reason }`) of the corresponding promise.
24-
*/
25-
allSettledKeyed<D extends Record<PropertyKey, unknown>>(promises: D): CoreJS.CoreJSPromise<{ [k in keyof D]: CoreJS.CoreJSPromiseSettledResult<Awaited<D[k]>> }>;
26-
}
27-
28-
var CoreJSPromise: CoreJSPromiseConstructor;
29-
}
1+
// empty
2+
// Moved to ../core-js-types/promise.d.ts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// empty
2+
// Moved to ../core-js-types/promise.d.ts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// empty
2+
// Moved to ../core-js-types/promise.d.ts

0 commit comments

Comments
 (0)