Last active
June 17, 2020 13:26
-
-
Save karol-majewski/4102688addd72aceaff0aa4b14931d24 to your computer and use it in GitHub Desktop.
Awaiting custom `Thenable<T>` in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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