Skip to content

Commit eb11b82

Browse files
committed
Return our own Promise type in pure types
1 parent 9dc8a5e commit eb11b82

23 files changed

+189
-96
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
declare namespace CoreJS {
2+
export interface CoreJSPromise<T> extends Promise<T> {}
3+
4+
export interface CoreJSPromiseConstructor {
5+
readonly prototype: CoreJSPromise<any>;
6+
7+
/**
8+
* Creates a new Promise.
9+
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
10+
* a resolve callback used to resolve the promise with a value or the result of another promise,
11+
* and a reject callback used to reject the promise with a provided reason or error.
12+
*/
13+
new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): CoreJSPromise<T>;
14+
15+
/**
16+
* Creates a Promise that is resolved with an array of results when all of the provided Promises
17+
* resolve, or rejected when any Promise is rejected.
18+
* @param values An array of Promises.
19+
* @returns A new Promise.
20+
*/
21+
all<T extends readonly unknown[] | []>(values: T): CoreJSPromise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>;
22+
23+
/**
24+
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
25+
* or rejected.
26+
* @param values An array of Promises.
27+
* @returns A new Promise.
28+
*/
29+
race<T extends readonly unknown[] | []>(values: T): CoreJSPromise<Awaited<T[number]>>;
30+
31+
/**
32+
* Creates a new rejected promise for the provided reason.
33+
* @param reason The reason the promise was rejected.
34+
* @returns A new rejected Promise.
35+
*/
36+
reject<T = never>(reason?: any): CoreJSPromise<T>;
37+
38+
/**
39+
* Creates a new resolved promise.
40+
* @returns A resolved promise.
41+
*/
42+
resolve(): CoreJSPromise<void>;
43+
/**
44+
* Creates a new resolved promise for the provided value.
45+
* @param value A promise.
46+
* @returns A promise whose internal state matches the provided promise.
47+
*/
48+
resolve<T>(value: T): CoreJSPromise<Awaited<T>>;
49+
/**
50+
* Creates a new resolved promise for the provided value.
51+
* @param value A promise.
52+
* @returns A promise whose internal state matches the provided promise.
53+
*/
54+
resolve<T>(value: T | PromiseLike<T>): CoreJSPromise<Awaited<T>>;
55+
}
56+
57+
var CoreJSPromise: CoreJSPromiseConstructor;
58+
}

packages/core-js-types/src/base/proposals/array-from-async.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface ArrayConstructor {
99
* Creates an array from an async iterator or iterable object.
1010
* @param iterableOrArrayLike An async iterator or array-like object to convert to an array.
1111
*/
12-
fromAsync<T>(iterableOrArrayLike: AsyncIterable<T> | Iterable<T | PromiseLike<T>> | ArrayLike<T | PromiseLike<T>>): Promise<T[]>;
12+
fromAsync<T>(iterableOrArrayLike: AsyncIterable<T> | Iterable<T | PromiseLike<T>> | ArrayLike<T | PromiseLike<T>>): Promise<T[]>; // @type-options prefix-return-type
1313

1414
/**
1515
* Creates an array from an async iterator or iterable object.
@@ -19,5 +19,5 @@ interface ArrayConstructor {
1919
* Each return value is awaited before being added to result array.
2020
* @param thisArg Value of 'this' used when executing mapFn.
2121
*/
22-
fromAsync<T, U>(iterableOrArrayLike: AsyncIterable<T> | Iterable<T> | ArrayLike<T>, mapFn: (value: Awaited<T>, index: number) => U, thisArg?: any): Promise<Awaited<U>[]>;
22+
fromAsync<T, U>(iterableOrArrayLike: AsyncIterable<T> | Iterable<T> | ArrayLike<T>, mapFn: (value: Awaited<T>, index: number) => U, thisArg?: any): Promise<Awaited<U>[]>; // @type-options prefix-return-type
2323
}

packages/core-js-types/src/base/proposals/explicit-resource-management.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ interface AsyncDisposableStack {
178178
*/
179179
move(): AsyncDisposableStack;
180180

181-
[Symbol.asyncDispose](): Promise<void>;
181+
[Symbol.asyncDispose](): Promise<void>; // @type-options prefix-return-type
182182

183183
readonly [Symbol.toStringTag]: string;
184184
}

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]>>; }>;
16+
allSettled<T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: CoreJS.CoreJSPromiseSettledResult<Awaited<T[P]>>; }>; // @type-options prefix-return-type
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>>[]>;
24+
allSettled<T>(values: Iterable<T | PromiseLike<T>>): Promise<CoreJS.CoreJSPromiseSettledResult<Awaited<T>>[]>; // @type-options prefix-return-type
2525
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ 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]>>;
13+
any<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>; // @type-options prefix-return-type
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>>;
20+
any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>; // @type-options prefix-return-type
2121
}
2222

2323
interface AggregateError extends Error { // @type-options no-redefine

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>;
14+
finally(onfinally?: (() => void) | undefined | null): Promise<T>; // @type-options prefix-return-type
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>>;
21+
try<T, U extends unknown[]>(callbackFn: (...args: U) => T | PromiseLike<T>, ...args: U): Promise<Awaited<T>>; // @type-options prefix-return-type
2222
}
2323

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// License: https://github.com/microsoft/TypeScript/blob/v5.9.3/LICENSE.txt
66

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

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

packages/core-js-types/src/base/pure/proposals/array-constructor.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference types="./iterator.d.ts" />
2+
/// <reference types="../../core-js-types/promise.d.ts" />
23

34
// Motivation: We must omit methods `fromAsync` and `isTemplateObject` because they cause a signature mismatch error.
45

@@ -20,7 +21,7 @@ declare namespace CoreJS {
2021
* Creates an array from an async iterator or iterable object.
2122
* @param iterableOrArrayLike An async iterator or array-like object to convert to an array.
2223
*/
23-
fromAsync<T>(iterableOrArrayLike: CoreJSAsyncIterable<T> | Iterable<T | PromiseLike<T>> | ArrayLike<T | PromiseLike<T>>): Promise<T[]>;
24+
fromAsync<T>(iterableOrArrayLike: CoreJSAsyncIterable<T> | Iterable<T | PromiseLike<T>> | ArrayLike<T | PromiseLike<T>>): CoreJSPromise<T[]>;
2425

2526
/**
2627
* Creates an array from an async iterator or iterable object.
@@ -30,7 +31,7 @@ declare namespace CoreJS {
3031
* Each return value is awaited before being added to result array.
3132
* @param thisArg Value of 'this' used when executing mapFn.
3233
*/
33-
fromAsync<T, U>(iterableOrArrayLike: CoreJSAsyncIterable<T> | Iterable<T> | ArrayLike<T>, mapFn: (value: Awaited<T>, index: number) => U, thisArg?: any): Promise<Awaited<U>[]>;
34+
fromAsync<T, U>(iterableOrArrayLike: CoreJSAsyncIterable<T> | Iterable<T> | ArrayLike<T>, mapFn: (value: Awaited<T>, index: number) => U, thisArg?: any): CoreJSPromise<Awaited<U>[]>;
3435

3536
/**
3637
* Determines whether an `value` is a `TemplateStringsArray`

packages/core-js-types/src/base/pure/proposals/async-iterator-helpers.d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="../../core-js-types/async-iterable.d.ts" />
22
/// <reference types="../../core-js-types/async-iterator-object.d.ts" />
3+
/// <reference types="../../core-js-types/promise.d.ts" />
34

45
// Motivation: Has dependencies on internal types, e.g. AsyncIterable, AsyncIteratorObject
56

@@ -30,7 +31,7 @@ declare namespace CoreJS {
3031
* @param predicate A function that tests each element of the iterator
3132
* @returns A promise that resolves to `true` if all elements pass the test, otherwise `false`
3233
*/
33-
every(predicate: (value: T, index: number) => boolean): Promise<boolean>;
34+
every(predicate: (value: T, index: number) => boolean): CoreJSPromise<boolean>;
3435

3536
/**
3637
* Creates a new `AsyncIterator` that contains only the elements that pass the `predicate` function.
@@ -44,7 +45,7 @@ declare namespace CoreJS {
4445
* @param predicate A function that tests each element of the iterator
4546
* @returns A `Promise` that resolves to the found element, or `undefined` if no element satisfies the `predicate`
4647
*/
47-
find(predicate: (value: T, index: number) => boolean): Promise<T>;
48+
find(predicate: (value: T, index: number) => boolean): CoreJSPromise<T>;
4849

4950
/**
5051
* Creates a new `AsyncIterator` by applying the `mapper` function to each element of the original iterator and flattening the result.
@@ -58,7 +59,7 @@ declare namespace CoreJS {
5859
* @param callbackFn A function that is called for each element of the iterator
5960
* @returns A `Promise` that resolves when all elements have been processed
6061
*/
61-
forEach(callbackFn: (value: T, index: number) => void): Promise<void>;
62+
forEach(callbackFn: (value: T, index: number) => void): CoreJSPromise<void>;
6263

6364
/**
6465
* Creates a new `AsyncIterator` by applying the `mapper` function to each element of the original iterator.
@@ -73,14 +74,14 @@ declare namespace CoreJS {
7374
* @param initialValue An optional initial value to start the reduction
7475
* @returns A `Promise` that resolves to the reduced value
7576
*/
76-
reduce(reducer: (accumulator: any, value: T, index: number) => any, initialValue?: any): Promise<any>;
77+
reduce(reducer: (accumulator: any, value: T, index: number) => any, initialValue?: any): CoreJSPromise<any>;
7778

7879
/**
7980
* Checks if any value in the iterator matches a given `predicate`
8081
* @param predicate A function that tests each element of the iterator
8182
* @returns A `Promise` that resolves to `true` if any element passes the `predicate`, otherwise `false`
8283
*/
83-
some(predicate: (value: T, index: number) => boolean): Promise<boolean>;
84+
some(predicate: (value: T, index: number) => boolean): CoreJSPromise<boolean>;
8485

8586
/**
8687
* Creates a new `AsyncIterator` that yields only the first `limit` elements from the original iterator.
@@ -93,7 +94,7 @@ declare namespace CoreJS {
9394
* Collects all elements from the iterator into an array.
9495
* @returns A `Promise` that resolves to an array containing all elements from the iterator
9596
*/
96-
toArray(): Promise<T[]>;
97+
toArray(): CoreJSPromise<T[]>;
9798
}
9899

99100
var CoreJSAsyncIterator: CoreJSAsyncIteratorConstructor;

0 commit comments

Comments
 (0)