Last active
February 19, 2021 13:22
-
-
Save safareli/7da1c0bd7b6063666ec609946c2897a9 to your computer and use it in GitHub Desktop.
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
type PVar<T> = Promise<T> & { | |
resolve: (res: T | PromiseLike<T>) => void; | |
reject: (reason?: unknown) => void; | |
}; | |
function mkPVar<T>() { | |
const result: PVar<T> = new Promise<T>((resolve, reject) => { | |
result.resolve = resolve; | |
result.reject = reject; | |
}) as PVar<T>; | |
return result; | |
} | |
const stringsP = mkPVar<string[]>() | |
setTimeout(() => { | |
stringsP.resolve(["asd"]) | |
// or `stringsP.reject("boom")` | |
}, 100) | |
await stringsP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment