UNPKG

775 BTypeScriptView Raw
1/**
2Create a type that represents either the value or the value wrapped in `PromiseLike`.
3
4Use-cases:
5- A function accepts a callback that may either return a value synchronously or may return a promised value.
6- This type could be the return type of `Promise#then()`, `Promise#catch()`, and `Promise#finally()` callbacks.
7
8Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31394) if you want to have this type as a built-in in TypeScript.
9
10@example
11```
12import {Promisable} from 'type-fest';
13
14async function logger(getLogEntry: () => Promisable<string>): Promise<void> {
15 const entry = await getLogEntry();
16 console.log(entry);
17}
18
19logger(() => 'foo');
20logger(() => Promise.resolve('bar'));
21```
22*/
23export type Promisable<T> = T | PromiseLike<T>;