/*! * @author electricessence / https://github.com/electricessence/ * Licensing: MIT * Although most of the following code is written from scratch, it is * heavily influenced by Q (https://github.com/kriskowal/q) and uses some of Q's spec. */ import { DisposableBase } from "../Disposable/DisposableBase"; import { Closure, Selector } from "../FunctionTypes"; export declare type Resolver = Selector, any> | null | undefined; export declare class PromiseState extends DisposableBase { protected _state: TSDNPromise.State; protected _result?: T | undefined; protected _error?: any; constructor(_state: TSDNPromise.State, _result?: T | undefined, _error?: any); protected _onDispose(): void; protected getState(): TSDNPromise.State; readonly state: TSDNPromise.State; readonly isPending: boolean; readonly isSettled: boolean; readonly isFulfilled: boolean; readonly isRejected: boolean; protected getResult(): T | undefined; readonly result: T | undefined; protected getError(): any; readonly error: any; } export declare abstract class PromiseBase extends PromiseState implements PromiseLike { protected constructor(); /** * .doneNow is provided as a non-standard means that synchronously resolves as the end of a promise chain. * As stated by promisejs.org: 'then' is to 'done' as 'map' is to 'forEach'. * It is the underlying method by which propagation occurs. * @param onFulfilled * @param onRejected */ abstract doneNow(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): void; /** * Calls the respective handlers once the promise is resolved. * @param onFulfilled * @param onRejected */ abstract thenSynchronous(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): PromiseBase; /** * Same as 'thenSynchronous' but does not return the result. Returns the current promise instead. * You may not need an additional promise result, and this will not create a new one. * @param onFulfilled * @param onRejected */ thenThis(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): this; /** * Standard .then method that defers execution until resolved. * @param onFulfilled * @param onRejected * @returns {TSDNPromise} */ then(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): PromiseBase; /** * Same as .then but doesn't trap errors. Exceptions may end up being fatal. * @param onFulfilled * @param onRejected * @returns {TSDNPromise} */ thenAllowFatal(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): PromiseBase; /** * .done is provided as a non-standard means that maps to similar functionality in other promise libraries. * As stated by promisejs.org: 'then' is to 'done' as 'map' is to 'forEach'. * @param onFulfilled * @param onRejected */ done(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): void; /** * Will yield for a number of milliseconds from the time called before continuing. * @param milliseconds * @returns A promise that yields to the current execution and executes after a delay. */ delayFromNow(milliseconds?: number): PromiseBase; /** * Will yield for a number of milliseconds from after this promise resolves. * If the promise is already resolved, the delay will start from now. * @param milliseconds * @returns A promise that yields to the current execution and executes after a delay. */ delayAfterResolve(milliseconds?: number): PromiseBase; /** * Shortcut for trapping a rejection. * @param onRejected * @returns {PromiseBase} */ 'catch'(onRejected: TSDNPromise.Reject): PromiseBase; /** * Shortcut for trapping a rejection but will allow exceptions to propagate within the onRejected handler. * @param onRejected * @returns {PromiseBase} */ catchAllowFatal(onRejected: TSDNPromise.Reject): PromiseBase; /** * Shortcut to for handling either resolve or reject. * @param fin * @returns {PromiseBase} */ 'finally'(fin: () => TSDNPromise.Resolution): PromiseBase; /** * Shortcut to for handling either resolve or reject but will allow exceptions to propagate within the handler. * @param fin * @returns {PromiseBase} */ finallyAllowFatal(fin: () => TSDNPromise.Resolution): PromiseBase; /** * Shortcut to for handling either resolve or reject. Returns the current promise instead. * You may not need an additional promise result, and this will not create a new one. * @param fin * @param synchronous * @returns {PromiseBase} */ finallyThis(fin: Closure, synchronous?: boolean): this; } export declare abstract class Resolvable extends PromiseBase { doneNow(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): void; thenSynchronous(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): PromiseBase; } /** * The simplest usable version of a promise which returns synchronously the resolved state provided. */ export declare abstract class Resolved extends Resolvable { protected constructor(state: TSDNPromise.State, result: T, error?: any); } /** * A fulfilled Resolved. Provided for readability. */ export declare class Fulfilled extends Resolved { constructor(value: T); } /** * A rejected Resolved. Provided for readability. */ export declare class Rejected extends Resolved { constructor(error: any); } /** * This promise class that facilitates pending resolution. */ export declare class TSDNPromise extends Resolvable { private _waiting; constructor(resolver?: TSDNPromise.Executor, forceSynchronous?: boolean); thenSynchronous(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): PromiseBase; doneNow(onFulfilled: TSDNPromise.Fulfill | undefined | null, onRejected?: TSDNPromise.Reject | null): void; protected _onDispose(): void; protected _resolvedCalled: boolean | undefined; resolveUsing(resolver: TSDNPromise.Executor, forceSynchronous?: boolean): void; private _emitDisposalRejection; private _resolveInternal; private _rejectInternal; resolve(result?: T | PromiseLike, throwIfSettled?: boolean): void; reject(error: any, throwIfSettled?: boolean): void; } /** * By providing an ArrayPromise we expose useful methods/shortcuts for dealing with array results. */ export declare class ArrayPromise extends TSDNPromise { /** * Simplifies the use of a map function on an array of results when the source is assured to be an array. * @param transform * @returns {PromiseBase>} */ map(transform: (value: T) => U): ArrayPromise; reduce(reduction: (previousValue: T, currentValue: T, i?: number, array?: T[]) => T, initialValue?: T): PromiseBase; reduce(reduction: (previousValue: U, currentValue: T, i?: number, array?: T[]) => U, initialValue: U): PromiseBase; static fulfilled(value: T[]): ArrayPromise; } /** * A Promise collection exposes useful methods for handling a collection of promises and their results. */ export declare class PromiseCollection extends DisposableBase { private readonly _source; constructor(source: PromiseLike[] | null | undefined); protected _onDispose(): void; /** * Returns a copy of the source promises. * @returns {PromiseLike>[]} */ readonly promises: PromiseLike[]; /** * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected. * @returns {PromiseBase} */ all(): ArrayPromise; /** * Creates a Promise that is resolved or rejected when any of the provided Promises are resolved * or rejected. * @returns {PromiseBase} A new Promise. */ race(): PromiseBase; /** * Returns a promise that is fulfilled with array of provided promises when all provided promises have resolved (fulfill or reject). * Unlike .all this method waits for all rejections as well as fulfillment. * @returns {PromiseBase[]>} */ waitAll(): ArrayPromise>; /** * Waits for all the values to resolve and then applies a transform. * @param transform * @returns {PromiseBase>} */ map(transform: (value: T) => U): ArrayPromise; /** * Applies a transform to each promise and defers the result. * Unlike map, this doesn't wait for all promises to resolve, ultimately improving the async nature of the request. * @param transform * @returns {PromiseCollection} */ pipe(transform: (value: T) => U | PromiseLike): PromiseCollection; reduce(reduction: (previousValue: T, currentValue: T, i?: number, array?: PromiseLike[]) => T, initialValue?: T | PromiseLike): PromiseBase; reduce(reduction: (previousValue: U, currentValue: T, i?: number, array?: PromiseLike[]) => U, initialValue: U | PromiseLike): PromiseBase; } export declare module TSDNPromise { /** * The state of a promise. * https://github.com/domenic/promises-unwrapping/blob/master/docs/states-and-fates.md * If a promise is disposed the value will be undefined which will also evaluate (promise.state)==false. */ enum State { Pending = 0, Fulfilled = 1, Rejected = -1 } type Resolution = TResult | PromiseLike; interface Fulfill { (value: T): Resolution; } interface Reject { (reason: any): Resolution; } interface Then { (onfulfilled?: Fulfill | undefined | null, onrejected?: Reject | undefined | null): PromiseLike; (onfulfilled?: Fulfill | undefined | null, onrejected?: Reject | undefined | null): PromiseLike; } interface Executor { (resolve: (value?: T | PromiseLike) => void, reject: (reason?: any) => void): void; } interface Factory { (executor: Executor): PromiseLike; } function factory(e: Executor): TSDNPromise; /** * Takes a set of promises and returns a PromiseCollection. * @param promises */ function group(promises: PromiseLike[]): PromiseCollection; function group(promise: PromiseLike, ...rest: PromiseLike[]): PromiseCollection; /** * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected. */ function all(promises: PromiseLike[]): ArrayPromise; function all(promise: PromiseLike, ...rest: PromiseLike[]): ArrayPromise; /** * Returns a promise that is fulfilled with array of provided promises when all provided promises have resolved (fulfill or reject). * Unlike .all this method waits for all rejections as well as fulfillment. */ function waitAll(promises: PromiseLike[]): ArrayPromise>; function waitAll(promise: PromiseLike, ...rest: PromiseLike[]): ArrayPromise>; /** * Creates a Promise that is resolved or rejected when any of the provided Promises are resolved * or rejected. * @param promises An array of Promises. * @returns A new Promise. */ function race(promises: PromiseLike[]): PromiseBase; function race(promise: PromiseLike, ...rest: PromiseLike[]): PromiseBase; /** * Creates a new resolved promise . * @returns A resolved promise. */ function resolve(): PromiseBase; /** * Creates a new resolved promise for the provided value. * @param value A value or promise. * @returns A promise whose internal state matches the provided promise. */ function resolve(value: T | PromiseLike): PromiseBase; /** * Syntactic shortcut for avoiding 'new'. * @param resolver * @param forceSynchronous * @returns {TSDNPromise} */ function using(resolver: TSDNPromise.Executor, forceSynchronous?: boolean): PromiseBase; /** * Takes a set of values or promises and returns a PromiseCollection. * Similar to 'group' but calls resolve on each entry. * @param resolutions */ function resolveAll(resolutions: Array>): PromiseCollection; function resolveAll(promise: T | PromiseLike, ...rest: Array>): PromiseCollection; /** * Creates a PromiseCollection containing promises that will resolve on the next tick using the transform function. * This utility function does not chain promises together to create the result, * it only uses one promise per transform. * @param source * @param transform * @returns {PromiseCollection} */ function map(source: T[], transform: (value: T) => U): PromiseCollection; /** * Creates a new rejected promise for the provided reason. * @param reason The reason the promise was rejected. * @returns A new rejected Promise. */ function reject(reason: T): PromiseBase; /** * Takes any Promise-Like object and ensures an extended version of it from this module. * @param target The Promise-Like object * @returns A new target that simply extends the target. */ function wrap(target: T | PromiseLike): PromiseBase; /** * A function that acts like a 'then' method (aka then-able) can be extended by providing a function that takes an onFulfill and onReject. * @param then * @returns {PromiseWrapper} */ function createFrom(then: Then): PromiseBase; } export { TSDNPromise as Promise }; export default TSDNPromise;