async-promise
Version:
Asynchronous coordination primitives for JavaScript and TypeScript
196 lines (195 loc) • 8.11 kB
TypeScript
declare module "async-promise" {
export interface IPromise<T> {
then<TResult>(onfulfilled: (value: T) => TResult | IPromise<TResult>, onrejected: (reason: any) => TResult | IPromise<TResult>): IPromise<TResult>;
}
/**
* Represents the completion of an asynchronous operation
*/
export class Promise<T> {
/**
* Creates a new Promise.
* @param init A callback used to initialize the promise. This callback is passed two arguments: a resolve callback used resolve the promise with a value or the result of another promise, and a reject callback used to reject the promise with a provided reason or error.
*/
constructor(init: (resolve: (value?: IPromise<T> | T) => void, reject: (reason?: any) => void) => void);
/**
* Creates a new resolved promise for the provided value.
* @param value A promise.
* @returns A promise whose internal state matches the provided promise.
*/
static resolve<T>(value: IPromise<T> | T): Promise<T>;
/**
* Creates a new resolved promise .
* @returns A resolved promise.
*/
static resolve(): Promise<void>;
/**
* Creates a new rejected promise for the provided reason.
* @param reason The reason the promise was rejected.
* @returns A new rejected Promise.
*/
static reject(reason: any): Promise<void>;
/**
* Creates a new rejected promise for the provided reason.
* @param reason The reason the promise was rejected.
* @returns A new rejected Promise.
*/
static reject<T>(reason: any): Promise<T>;
/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
static all<T>(values: (IPromise<T> | T)[]): Promise<T[]>;
/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
static race<T>(values: (IPromise<T> | T)[]): Promise<T>;
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: T) => IPromise<TResult> | TResult, onrejected?: (reason: any) => IPromise<TResult> | TResult): Promise<TResult>;
/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch(onrejected: (reason: any) => IPromise<T> | T): Promise<T>;
/**
* Attaches a callback for that is executed regardless of the resolution or rejection of the promise.
* @param onsettled The callback to execute when the Promise is settled.
* @returns A Promise for the completion of the callback.
*/
finally(onsettled: () => IPromise<void> | void): Promise<T>;
private _resolve(result);
private _await(onresolved, onrejected);
private _settle(rejecting, result);
private _forward(prev, resolve, reject, rejecting, result, onresolved, onrejected, id);
}
export class Task {
private _onexecute;
private _oncancel;
constructor(onexecute: () => void, oncancel?: (reason: any) => void);
execute(): void;
cancel(reason?: any): void;
}
export enum TaskSchedulerPriority {
none = -1,
background = 0,
send = 1,
}
export class TaskScheduler {
private static _default;
private _state;
private _channel;
private _handle;
private _backgroundQueue;
private _sendQueue;
private _marker;
private _marked;
private _ontick;
constructor();
static default: TaskScheduler;
max: TaskSchedulerPriority;
enqueue(priority: TaskSchedulerPriority, onexecute: () => void, oncancel?: (reason: any) => void): Task;
delay(msec: number, onexecute: () => void, oncancel?: (reason: any) => void): Task;
private requestTick();
private resetTick();
private tick();
private getQueue(priority);
private dequeue();
private onexecute(priority, node, onexecute);
private oncancel(priority, node, oncancel, reason);
private ondelayexecute(handle, onexecute);
private ondelaycancel(handle, oncancel, reason);
}
/**
* A source for cancellation
*/
export class CancellationTokenSource {
private static _canceled;
private _callbacks;
private _links;
private _token;
private _canceled;
private _reason;
/**
* @param links Other `CancellationToken` instances that will cancel this source if the tokens are canceled.
*/
constructor(links?: CancellationToken[]);
/**
* Gets the `CancellationToken` for this source.
*/
token: CancellationToken;
/**
* Signals the source is cancelled.
* @param reason An optional reason for the cancellation.
*/
cancel(reason?: any): void;
/**
* Closes the CancellationSource, preventing any future cancellation signal.
*/
close(): void;
private _register(callback);
private _enqueueCallback(callback);
private _invokeCallback(callback, id);
private _throwIfFrozen();
}
/**
* A token used to recieve a cancellation signal.
*/
export class CancellationToken {
private static _none;
private _source;
constructor(source: CancellationTokenSource);
/**
* Gets an empty cancellation token that will never be canceled.
*/
static none: CancellationToken;
/**
* Gets a value indicating whether the token can be canceled.
*/
canBeCanceled: boolean;
/**
* Gets a value indicating whether the token has received a cancellation signal.
*/
canceled: boolean;
/**
* Gets the reason for cancellation, if one was supplied.
*/
reason: any;
/**
* Throws an `Error` if the token has received a cancellation signal.
*/
throwIfCanceled(reason?: any): void;
/**
* Requests a callback when the token receives a cancellation signal to perform additional cleanup.
* @param callback The callback to execute
* @returns A `CancellationTokenRegistration` that that can be used to cancel the cleanup request.
*/
register(callback: (reason: any) => void): CancellationTokenRegistration;
}
/**
* An object used to unregister a callback delegate registered to a `CancellationToken`
*/
export interface CancellationTokenRegistration {
/**
* Unregisters the callback
*/
unregister(): void;
}
export class Deferred<T> {
private _promise;
private _resolve;
private _reject;
constructor();
promise: Promise<T>;
resolve(value?: IPromise<T> | T): void;
reject(reason: any): void;
}
export function sleep(delay?: number, token?: CancellationToken): Promise<void>;
}