-
Notifications
You must be signed in to change notification settings - Fork 12.8k
fix(60223): add Promise.try() to ESNext lib #60232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d6e6d2
fix(60223): add Promise.try() to ESNext lib
dirkluijk 69b6cbb
Allow PromiseLike results
dirkluijk 699170e
Allow passing additional args
dirkluijk 3af5da0
Fixed incorrect description in JSDoc
dirkluijk 57e3f87
fix: flatten return type of Promise.try()
dirkluijk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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 with no arguments. It can do anything: either return | ||
* a value, throw an error, or return a promise. | ||
* | ||
* @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>(callbackFn: () => T | Promise<T>): Promise<T>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//// [tests/cases/compiler/promiseTry.ts] //// | ||
|
||
//// [promiseTry.ts] | ||
Promise.try(() => { | ||
return "Sync result"; | ||
}); | ||
|
||
Promise.try(async () => { | ||
return "Async result"; | ||
}); | ||
|
||
|
||
//// [promiseTry.js] | ||
Promise.try(() => { | ||
return "Sync result"; | ||
}); | ||
Promise.try(async () => { | ||
return "Async result"; | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//// [tests/cases/compiler/promiseTry.ts] //// | ||
|
||
=== promiseTry.ts === | ||
Promise.try(() => { | ||
>Promise.try : Symbol(PromiseConstructor.try, Decl(lib.esnext.promise.d.ts, --, --)) | ||
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) | ||
>try : Symbol(PromiseConstructor.try, Decl(lib.esnext.promise.d.ts, --, --)) | ||
|
||
return "Sync result"; | ||
}); | ||
|
||
Promise.try(async () => { | ||
>Promise.try : Symbol(PromiseConstructor.try, Decl(lib.esnext.promise.d.ts, --, --)) | ||
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) | ||
>try : Symbol(PromiseConstructor.try, Decl(lib.esnext.promise.d.ts, --, --)) | ||
|
||
return "Async result"; | ||
}); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//// [tests/cases/compiler/promiseTry.ts] //// | ||
|
||
=== promiseTry.ts === | ||
Promise.try(() => { | ||
>Promise.try(() => { return "Sync result";}) : Promise<string> | ||
> : ^^^^^^^^^^^^^^^ | ||
>Promise.try : <T>(callbackFn: () => T | Promise<T>) => Promise<T> | ||
> : ^ ^^ ^^ ^^^^^ | ||
>Promise : PromiseConstructor | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
>try : <T>(callbackFn: () => T | Promise<T>) => Promise<T> | ||
> : ^ ^^ ^^ ^^^^^ | ||
>() => { return "Sync result";} : () => string | ||
> : ^^^^^^^^^^^^ | ||
|
||
return "Sync result"; | ||
>"Sync result" : "Sync result" | ||
> : ^^^^^^^^^^^^^ | ||
|
||
}); | ||
|
||
Promise.try(async () => { | ||
>Promise.try(async () => { return "Async result";}) : Promise<string> | ||
> : ^^^^^^^^^^^^^^^ | ||
>Promise.try : <T>(callbackFn: () => T | Promise<T>) => Promise<T> | ||
> : ^ ^^ ^^ ^^^^^ | ||
>Promise : PromiseConstructor | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
>try : <T>(callbackFn: () => T | Promise<T>) => Promise<T> | ||
> : ^ ^^ ^^ ^^^^^ | ||
>async () => { return "Async result";} : () => Promise<string> | ||
> : ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
return "Async result"; | ||
>"Async result" : "Async result" | ||
> : ^^^^^^^^^^^^^^ | ||
|
||
}); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.