-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce new
afterMaybeAsync
helper
`after` becomes sync, requiring no return type check for the onFulfilled result.
- Loading branch information
Showing
4 changed files
with
29 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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,21 @@ | ||
import { isPromise } from './isPromise.js'; | ||
import type { PromiseOrValue } from './PromiseOrValue.js'; | ||
|
||
/** | ||
* Async Helper Function that avoides `.then()` | ||
* | ||
* It is faster to await a promise prior to returning it from an async function | ||
* than to return a promise with `.then()`. | ||
* | ||
* see: https://github.com/tc39/proposal-faster-promise-adoption | ||
*/ | ||
export async function afterMaybeAsync<T, R>( | ||
promise: Promise<T>, | ||
onFulfilled: (value: T) => PromiseOrValue<R>, | ||
): Promise<R> { | ||
const result = onFulfilled(await promise); | ||
if (isPromise(result)) { | ||
return await result; | ||
} | ||
return result; | ||
} |
This file contains 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