import { Thenable } from '@dojo/shim/interfaces'; import { Iterable } from '@dojo/shim/iterator'; import { Executor } from '@dojo/shim/Promise'; import ExtensiblePromise, { DictionaryOfPromises, ListOfPromises } from './ExtensiblePromise'; /** * Describe the internal state of a task. */ export declare const enum State { Fulfilled = 0, Pending = 1, Rejected = 2, Canceled = 3, } /** * A type guard that determines if `value` is a `Task` * @param value The value to guard */ export declare function isTask(value: any): value is Task; /** * Returns true if a given value has a `then` method. * @param {any} value The value to check if is Thenable * @returns {is Thenable} A type guard if the value is thenable */ export declare function isThenable(value: any): value is Thenable; /** * Task is an extension of Promise that supports cancellation and the Task#finally method. */ export default class Task extends ExtensiblePromise { /** * Return a Task that resolves when one of the passed in objects have resolved * * @param iterable An iterable of values to resolve. These can be Promises, ExtensiblePromises, or other objects * @returns {Task} */ static race(iterable: Iterable> | (T | Thenable)[]): Task; /** * Return a rejected promise wrapped in a Task * * @param reason The reason for the rejection * @returns A task */ static reject(reason?: Error): Task; /** * Return a resolved task. * * @param value The value to resolve with * * @return A task */ static resolve(): Task; /** * Return a resolved task. * * @param value The value to resolve with * * @return A task */ static resolve(value: T | Thenable): Task; /** * Return a ExtensiblePromise that resolves when all of the passed in objects have resolved. When used with a key/value * pair, the returned promise's argument is a key/value pair of the original keys with their resolved values. * * @example * ExtensiblePromise.all({ one: 1, two: 2 }).then(results => console.log(results)); * // { one: 1, two: 2 } * * @param iterable An iterable of values to resolve, or a key/value pair of values to resolve. These can be Promises, ExtensiblePromises, or other objects * @returns An extensible promise */ static all(iterable: DictionaryOfPromises): Task<{ [key: string]: T; }>; /** * Return a ExtensiblePromise that resolves when all of the passed in objects have resolved. When used with a key/value * pair, the returned promise's argument is a key/value pair of the original keys with their resolved values. * * @example * ExtensiblePromise.all({ one: 1, two: 2 }).then(results => console.log(results)); * // { one: 1, two: 2 } * * @param iterable An iterable of values to resolve, or a key/value pair of values to resolve. These can be Promises, ExtensiblePromises, or other objects * @returns An extensible promise */ static all(iterable: (T | Thenable)[]): Task; /** * Return a ExtensiblePromise that resolves when all of the passed in objects have resolved. When used with a key/value * pair, the returned promise's argument is a key/value pair of the original keys with their resolved values. * * @example * ExtensiblePromise.all({ one: 1, two: 2 }).then(results => console.log(results)); * // { one: 1, two: 2 } * * @param iterable An iterable of values to resolve, or a key/value pair of values to resolve. These can be Promises, ExtensiblePromises, or other objects * @returns An extensible promise */ static all(iterable: T | Thenable): Task; /** * Return a ExtensiblePromise that resolves when all of the passed in objects have resolved. When used with a key/value * pair, the returned promise's argument is a key/value pair of the original keys with their resolved values. * * @example * ExtensiblePromise.all({ one: 1, two: 2 }).then(results => console.log(results)); * // { one: 1, two: 2 } * * @param iterable An iterable of values to resolve, or a key/value pair of values to resolve. These can be Promises, ExtensiblePromises, or other objects * @returns An extensible promise */ static all(iterable: ListOfPromises): Task; /** * A cancelation handler that will be called if this task is canceled. */ private canceler; /** * Children of this Task (i.e., Tasks that were created from this Task with `then` or `catch`). */ private readonly children; /** * The finally callback for this Task (if it was created by a call to `finally`). */ private _finally; /** * The state of the task */ protected _state: State; readonly state: State; /** * @constructor * * Create a new task. Executor is run immediately. The canceler will be called when the task is canceled. * * @param executor Method that initiates some task * @param canceler Method to call when the task is canceled * */ constructor(executor: Executor, canceler?: () => void); /** * Propagates cancellation down through a Task tree. The Task's state is immediately set to canceled. If a Thenable * finally task was passed in, it is resolved before calling this Task's finally callback; otherwise, this Task's * finally callback is immediately executed. `_cancel` is called for each child Task, passing in the value returned * by this Task's finally callback or a Promise chain that will eventually resolve to that value. */ private _cancel(finallyTask?); /** * Immediately cancels this task if it has not already resolved. This Task and any descendants are synchronously set * to the Canceled state and any `finally` added downstream from the canceled Task are invoked. */ cancel(): void; catch(onRejected?: ((reason: any) => TResult | PromiseLike) | undefined): Task; /** * Allows for cleanup actions to be performed after resolution of a Promise. */ finally(callback: () => void): Task; /** * Adds a callback to be invoked when the Task resolves or is rejected. * * @param onFulfilled A function to call to handle the resolution. The paramter to the function will be the resolved value, if any. * @param onRejected A function to call to handle the error. The parameter to the function will be the caught error. * * @returns A task */ then(onFulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onRejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): Task; }