Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Last active June 17, 2020 13:26
Show Gist options
  • Save karol-majewski/4102688addd72aceaff0aa4b14931d24 to your computer and use it in GitHub Desktop.
Save karol-majewski/4102688addd72aceaff0aa4b14931d24 to your computer and use it in GitHub Desktop.
Awaiting custom `Thenable<T>` in TypeScript
interface Thenable<T> {
then<U>(onResolve: (value: T) => U, onReject: (error: any) => Thenable<U>): Thenable<U>
then<U>(onResolve: (value: T) => Thenable<U>, onReject: (error: any) => Thenable<U>): Thenable<U>
}
const client: Thenable<string> = {
async then(resolve: (value: string) => Promise<string>, reject: (error: string) => Promise<string>) {
return Math.random() > 0.5
? resolve('Success!')
: reject('Failure!')
}
}
const result = await client;
/* Enable top-level await */
export { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment