parallel-es
Version:
Simple parallelization for EcmaScript
39 lines (38 loc) • 1.6 kB
TypeScript
import { ITask } from "./task";
import { ITaskDefinition } from "./task-definition";
/**
* Default implementation of a task. Keeps the state of a state and allows to register handlers that are invoked
* in case of task completion or error.
*/
export declare class WorkerTask<T> implements ITask<T> {
definition: ITaskDefinition;
isCancellationRequested: boolean;
isCanceled: boolean;
private resolvePromise;
private rejectPromise;
private promise;
/**
* Creates a new worker task that executes the operation defined by the given definition
* @param definition the definition of the operation to execute
*/
constructor(definition: ITaskDefinition);
/**
* Marks the task as complete where the given value is the task result. Triggers all registered `then` handlers of the promise.
* However, if the task has been cancelled in the mean time, then the `catch` handlers are triggered instead
* @param result the result of the task execution
*/
resolve(result: T): void;
/**
* Resolves a task that has been cancelled before start.
* Triggers all the `catch` handlers.
*/
resolveCancelled(): void;
/**
* Marks the task as failed
* @param error the error that occurred while processing the task
*/
reject(error: any): void;
then<TResult>(onfulfilled: (value: T) => (PromiseLike<TResult> | TResult), onrejected?: (reason: any) => (PromiseLike<TResult> | TResult)): Promise<TResult>;
catch(onrejected: (reason: any) => (PromiseLike<any> | any)): Promise<any>;
cancel(): void;
}