Add a new `type Awaitable<T> = T | PromiseLike<T>`
See original GitHub issueSearch Terms
awaitable
Suggestion
Add an awaitable type for values that will be awaited.
type Awaitable<T> = T | PromiseLike<T>;
This is based on my question and a comment on StackOverflow
Use Cases
Two use cases come in mind immediately:
- A function accepts a callback that may either return a value synchronously, or may return a promise value. This will then probably be awaited.
- This is more of a specific version of 1, but this would be the return type of
Promise.then()
/Promise.catch
/Promise.finally
callbacks.
Also, this type could replace all 1334 occurrences that come up when running git grep '| Promise'
in the current TypeScript code base.
Examples
Callback example:
async function logAnswer(getAnswer: () => Awaitable<number>): Promise<void> {
const answer = await getAnswer();
console.log(answer);
}
logAnswer(() => 42);
logAnswer(() => Promise.resolve(42));
Promise example:
Promise.resolve('Hello, world!').then(
// This type annotation is silly. This is really just to show promise callbacks should accept `Awaitable<T>`.
(hello): Awaitable<string> => {
console.log(hello);
return hello;
},
);
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:54
- Comments:7
Top Results From Across the Web
TypeScript based Promise/A+ compliant, awaitable promise ...
This is yet another Node.js Promise implementation, but this is written in TypeScript, and it strives for type safety and clarity.
Read more >extra-promise - npm
TypeScript icon, indicating that this package has built-in type ... function pad<T>(ms: number, fn: () => Awaitable<T>): Promise<T>.
Read more >Typescript: Why is PromiseLike a possible type of this in an ...
It tells me that this is either of type MyType or a PromiseLike<MyType> . The second possible type is the problem but I...
Read more >PromiseLike | typescript - v3.7.7
Interface PromiseLike<T>. Type parameters. T. Hierarchy. PromiseLike.
Read more >ajv-validator/ajv - Gitter
Type 'U | Thenable<U>' is not assignable to type 'U | PromiseLike<U>'. ... adding meta schema to the instance doesn't really switch ajv...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think
Promisable
would be a better name thanAwaitable
.Since any value is awaitable in TS this name seems too generic to me,
Promisable
on the other hand says to me that the value could be actually a Promise (technically aPromiseLike
, butPromiseLikeable
orPromiseableLike
sound awful to me).Some feedback from future self: I have been using
Promisable
a lot. I really hope this will be a builtin type.