typepool
Version:
Object pooling inspired by generic-pool - with Typing
45 lines (44 loc) • 1.72 kB
TypeScript
export interface DeferredResolver<R> {
/**
* Returns a reference to the controlled promise that can be passed to clients.
*/
promise: Promise<R>;
/**
* Resolve the underlying promise with `value` as the resolution value. If `value` is a thenable or a promise, the underlying promise will assume its state.
*/
resolve(value: R): void;
resolve(): void;
/**
* Reject the underlying promise with `reason` as the rejection reason.
*/
reject(reason: any): void;
/**
* Gives you a callback representation of the `PromiseResolver`. Note that this is not a method but a property.
* The callback accepts error object in first argument and success values on the 2nd parameter and the rest, I.E. node js conventions.
*
* If the the callback is called with multiple success values, the resolver fulfills its promise with an array of the values.
*/
callback(err: any, value: R, ...values: R[]): void;
}
/**
* A deferred promise that can be resolved or rejected
* externally, ideal for functions like a promise timeout
*/
export declare class Deferred<T> {
static delay(millis: number): Promise<void>;
static resolve<T>(value?: T): Deferred<T>;
private result;
private cancelCallback;
private state;
readonly promise: Promise<T>;
constructor(promise?: Promise<T> | undefined);
isSettled(): boolean;
isCancelled(): boolean;
cancel(): void;
getResult(): T;
onCancel(cancelCallback: DeferredCancelCallback<T>): void;
resolve(result?: T | undefined): void;
reject(err: any): void;
}
export declare type DeferredCancelCallback<T> = (deferred: Deferred<T>) => void;
export default Deferred;