question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a new `type Awaitable<T> = T | PromiseLike<T>`

See original GitHub issue

Search 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:

  1. A function accepts a callback that may either return a value synchronously, or may return a promise value. This will then probably be awaited.
  2. 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:open
  • Created 4 years ago
  • Reactions:54
  • Comments:7

github_iconTop GitHub Comments

4reactions
fabiospampinatocommented, Jul 2, 2019

I think Promisable would be a better name than Awaitable.

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 a PromiseLike, but PromiseLikeable or PromiseableLike sound awful to me).

3reactions
remcohaszingcommented, Apr 6, 2020

Some feedback from future self: I have been using Promisable a lot. I really hope this will be a builtin type.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found