Skip to content

Instantly share code, notes, and snippets.

@safareli
Last active February 19, 2021 13:22
Show Gist options
  • Save safareli/7da1c0bd7b6063666ec609946c2897a9 to your computer and use it in GitHub Desktop.
Save safareli/7da1c0bd7b6063666ec609946c2897a9 to your computer and use it in GitHub Desktop.
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