mirror of
https://github.com/denoland/deno.git
synced 2024-11-27 16:10:57 -05:00
43 lines
899 B
TypeScript
43 lines
899 B
TypeScript
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||
|
|
||
|
export type Deferred<T = any, R = Error> = {
|
||
|
promise: Promise<T>;
|
||
|
resolve: (t?: T) => void;
|
||
|
reject: (r?: R) => void;
|
||
|
readonly handled: boolean;
|
||
|
};
|
||
|
|
||
|
/** Create deferred promise that can be resolved and rejected by outside */
|
||
|
export function defer<T>(): Deferred<T> {
|
||
|
let handled = false;
|
||
|
let resolve;
|
||
|
let reject;
|
||
|
const promise = new Promise<T>((res, rej) => {
|
||
|
resolve = r => {
|
||
|
handled = true;
|
||
|
res(r);
|
||
|
};
|
||
|
reject = r => {
|
||
|
handled = true;
|
||
|
rej(r);
|
||
|
};
|
||
|
});
|
||
|
return {
|
||
|
promise,
|
||
|
resolve,
|
||
|
reject,
|
||
|
get handled() {
|
||
|
return handled;
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export function isDeferred(x): x is Deferred {
|
||
|
return (
|
||
|
typeof x === "object" &&
|
||
|
x.promise instanceof Promise &&
|
||
|
typeof x["resolve"] === "function" &&
|
||
|
typeof x["reject"] === "function"
|
||
|
);
|
||
|
}
|