Skip to content
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

Add Promisable type #45

Merged
merged 6 commits into from
Jul 5, 2019
Merged

Add Promisable type #45

merged 6 commits into from
Jul 5, 2019

Conversation

kainiedziela
Copy link
Contributor

@kainiedziela kainiedziela commented Jul 3, 2019

Fixes #40
TypeScript proposal: #31394

A type for values that can either be a promise or it's return value.

type Promisable<T> = T | PromiseLike<T>;

Example

async function logger(getLogEntry: () => Awaitable<string>): Promise<void> {
    const entry = await getLogEntry();
    console.log(entry);
}

logger(() => 'foo');
logger(() => Promise.resolve('bar'));

Use cases

  • A function accepts a callback that may either return a value synchronously or may return a promised value.
  • Specifically this type could be the return type of Promise.then(), Promise.catch() and Promise.finally() callbacks.

source/promisable.d.ts Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
source/promisable.d.ts Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: Promisable
2 participants