Skip to content

Commit

Permalink
fix(60223): add Promise.try() to ESNext lib (#60232)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkluijk authored Nov 6, 2024
1 parent 80eeb4e commit 60dd512
Show file tree
Hide file tree
Showing 79 changed files with 1,525 additions and 740 deletions.
1 change: 1 addition & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ const libEntries: [string, string][] = [
["esnext.regexp", "lib.es2024.regexp.d.ts"],
["esnext.string", "lib.es2024.string.d.ts"],
["esnext.iterator", "lib.esnext.iterator.d.ts"],
["esnext.promise", "lib.esnext.promise.d.ts"],
["decorators", "lib.decorators.d.ts"],
["decorators.legacy", "lib.decorators.legacy.d.ts"],
];
Expand Down
1 change: 1 addition & 0 deletions src/lib/esnext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/// <reference lib="esnext.collection" />
/// <reference lib="esnext.array" />
/// <reference lib="esnext.iterator" />
/// <reference lib="esnext.promise" />
16 changes: 16 additions & 0 deletions src/lib/esnext.promise.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface PromiseConstructor {
/**
* Takes a callback of any kind (returns or throws, synchronously or asynchronously) and wraps its result
* in a Promise.
*
* @param callbackFn A function that is called synchronously. It can do anything: either return
* a value, throw an error, or return a promise.
* @param args Additional arguments, that will be passed to the callback.
*
* @returns A Promise that is:
* - Already fulfilled, if the callback synchronously returns a value.
* - Already rejected, if the callback synchronously throws an error.
* - Asynchronously fulfilled or rejected, if the callback returns a promise.
*/
try<T, U extends unknown[]>(callbackFn: (...args: U) => T | PromiseLike<T>, ...args: U): Promise<Awaited<T>>;
}
1 change: 1 addition & 0 deletions src/lib/libs.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"esnext.collection",
"esnext.array",
"esnext.iterator",
"esnext.promise",
"decorators",
"decorators.legacy",
// Default libraries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,19 @@
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,21 @@
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/modulePreserve2.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,18 @@
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/modulePreserve3.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,17 @@
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,20 @@
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,21 @@
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,20 @@
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules/@types' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
64 changes: 64 additions & 0 deletions tests/baselines/reference/promiseTry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//// [tests/cases/compiler/promiseTry.ts] ////

//// [promiseTry.ts]
Promise.try(() => {
return "Sync result";
});

Promise.try(async () => {
return "Async result";
});

const a = Promise.try(() => "Sync result");
const b = Promise.try(async () => "Async result");

// SINGLE PARAMETER
Promise.try((foo: string) => "Async result", "foo");
Promise.try((foo) => "Async result", "foo");
// @ts-expect-error too few parameters
Promise.try((foo) => "Async result");
Promise.try((foo: string | undefined) => "Async result", undefined);
Promise.try((foo: string | undefined) => "Async result", "foo");
Promise.try((foo) => "Async result", undefined);
// @ts-expect-error too many parameters
Promise.try(() => "Async result", "foo");

// MULTIPLE PARAMETERS
Promise.try((foo: string, bar: number) => "Async result", "foo", 42);
// @ts-expect-error too many parameters
Promise.try((foo: string, bar: number) => "Async result", "foo", 42, "baz");
// @ts-expect-error too few parameters
Promise.try((foo: string, bar: number) => "Async result", "foo");
Promise.try((foo: string, bar?: number) => "Async result", "foo");
Promise.try((foo: string, bar?: number) => "Async result", "foo", undefined);
Promise.try((foo: string, bar?: number) => "Async result", "foo", 42);


//// [promiseTry.js]
Promise.try(() => {
return "Sync result";
});
Promise.try(async () => {
return "Async result";
});
const a = Promise.try(() => "Sync result");
const b = Promise.try(async () => "Async result");
// SINGLE PARAMETER
Promise.try((foo) => "Async result", "foo");
Promise.try((foo) => "Async result", "foo");
// @ts-expect-error too few parameters
Promise.try((foo) => "Async result");
Promise.try((foo) => "Async result", undefined);
Promise.try((foo) => "Async result", "foo");
Promise.try((foo) => "Async result", undefined);
// @ts-expect-error too many parameters
Promise.try(() => "Async result", "foo");
// MULTIPLE PARAMETERS
Promise.try((foo, bar) => "Async result", "foo", 42);
// @ts-expect-error too many parameters
Promise.try((foo, bar) => "Async result", "foo", 42, "baz");
// @ts-expect-error too few parameters
Promise.try((foo, bar) => "Async result", "foo");
Promise.try((foo, bar) => "Async result", "foo");
Promise.try((foo, bar) => "Async result", "foo", undefined);
Promise.try((foo, bar) => "Async result", "foo", 42);
Loading

0 comments on commit 60dd512

Please sign in to comment.