import { Validation } from "../util"; export declare type Reject = (error: E) => void; export declare type Resolve = (result: S) => void; export declare type Fork = (reject: Reject, resolve: Resolve) => void; /** * Creates a Task which can be resolved/rejected externally. */ export declare const external: () => ExternalTask; export declare const emitter: (fn: (...args: Args) => R) => [ExternalTask, (...args: Args) => void]; /** * Creates a Task which has already successfully completed with `result`. * @alias of * @alias ok * @param result The value to place into the successful Task. */ export declare const succeed: (result: S) => Task; export declare const of: (result: S) => Task; /** * Creates a Task which succeeds when forked. * @param result The function which will produce the result. */ export declare const succeedBy: (result: () => S) => Task; export declare const try_: (result: () => S) => Task; /** * Creates a Task has an empty result. * @alias unit */ export declare const empty: () => Task; /** * Creates a Task which automatically succeeds at some time in the future with `result`. * @param ms How many milliseconds until it succeeds. * @param result The value to place into the successful Task. */ export declare const succeedIn: (ms: number, result: S) => Task; /** * Creates a Task which has already failed with `error`. * @alias err * @param error The error to place into the failed Task. */ export declare const fail: (error: E) => Task; /** * Creates a Task which automatically fails at some time in the future with `error`. * @param ms How many milliseconds until it succeeds. * @param error The error to place into the failed Task. */ export declare const failIn: (ms: number, error: E) => Task; /** * Creates a Task will never finish. */ export declare const never: () => Task; /** * Execute task computation and call handlers on completion. * @param reject Function to call on failure. * @param resolve Function to call on success. * @param task The task to fork. */ export declare const fork: (reject: Reject, resolve: Resolve, task: Task) => { cancel: () => void; }; /** * Chain a task to run after a previous task has succeeded. * @param fn Takes a successful result and returns a new task. * @param task The task which will chain to the next one on success. */ export declare const chain: (fn: (result: S) => Task, task: Task) => Task; /** * When forked, run a function which can check whether the task has already succeeded. * @param fn The function which either returns a success value or undefined. * @param task The task to run if the check fails (returns undefined). */ export declare const succeedIf: (fn: () => S | undefined, task: Task) => Task; /** * A task which only runs once. Caches the success or failure. Be careful. * @alias share * @param task The task to cache results. */ export declare const onlyOnce: (task: Task) => Task; export declare const share: (task: Task) => Task; /** * Given a promise, create a Task which relies on it. * @param promise The promise we will gather the success from. */ export declare const fromPromise: (maybePromise: S | Promise) => Task; /** * Given an array of promises, create a Task which relies on it. * @param promise The promises we will gather the success from. */ export declare const fromPromises: (promises: Promise[]) => Task; /** * Take a function which generates a promise and lazily execute it. * @param getPromise The getter function */ export declare const fromLazyPromise: (getPromise: () => S | Promise) => Task; /** * Given a function that returns a promise, return a new function that * lazily returns a Task instead. * @param fn A function which returns a promise */ export declare const wrapPromiseCreator: (fn: (...args: Args) => Promise) => (...args: Args) => Task; /** * Given a task, create a Promise which resolves when the task does. * @param task The task we will convert to a promise. */ export declare const toPromise: (task: Task) => Promise; /** * Given an array of tasks, return the one which finishes first. * @alias select * @param tasks The tasks to run in parallel. */ export declare const race: (tasks: Task[]) => Task; export declare class LoopBreak { readonly value: S; constructor(value: S); } export declare class LoopContinue { readonly value: S; constructor(value: S); } /** * Given an initialValue, asynchronously loop until either a value is * resolved by returning a Task>. * @param fn A function that takes the current loop value and decides whether to continue or break. * @param initialValue The initial value. */ export declare const loop: (fn: (currentValue: T) => Task | LoopContinue>, initialValue: T) => Task; /** * An async reducer. Given an initial return value and an array of * items to sequentially loop over, pass each step through a reducer * function which returns a Task of the next reduced value. * @param fn * @param initialValue * @param items */ export declare const reduce: (fn: (acc: V, currentValue: T, index: number, original: T[]) => Task, initialValue: V, items: T[]) => Task; /** * Given an array of tasks, return the one which finishes successfully first. * @param tasks The tasks to run in parallel. */ export declare const firstSuccess: (tasks: Task[]) => Task; /** * Given an array of task which return a result, return a new task which returns an array of results. * @alias collect * @param tasks The tasks to run in parallel. */ export declare const all: (tasks: Task[]) => Task; /** * Given an array of task which return a result, return a new task which returns an array of successful results. * @param tasks The tasks to run in parallel. */ export declare const allSuccesses: (tasks: Task[]) => Task; /** * Creates a task that waits for two tasks of different types to * resolve as a two-tuple of the results. * @param taskA The first task. * @param taskB The second task. */ export declare const zip: (taskA: Task, taskB: Task) => Task; /** * Creates a task that waits for two tasks of different types to * resolve, then passing the resulting two-tuple of results through * a mapping function. * @param fn * @param taskA The first task. * @param taskB The second task. */ export declare const zipWith: (fn: (resultA: S, resultB: S2) => V, taskA: Task, taskB: Task) => Task; /** * Given an array of task which return a result, return a new task which results an array of results. * @param tasks The tasks to run in sequence. */ export declare const sequence: (tasks: Task[], maxConcurrent?: number) => Task; /** * Given a task, swap the error and success values. * @param task The task to swap the results of. */ export declare const swap: (task: Task) => Task; /** * Given a task, map the successful value to a Task. * @param fn A function which takes the original successful result and returns the new one. * @param task The task to map the succcessful result. */ export declare const map: (fn: (result: S) => S2, task: Task) => Task; export declare const map2: (fn: (a: S) => (b: S2) => S3, taskA: Task, taskB: Task) => Task; export declare const map3: (fn: (a: S) => (b: S2) => (c: S3) => S4, taskA: Task, taskB: Task, taskC: Task) => Task; export declare const map4: (fn: (a: S) => (b: S2) => (c: S3) => (d: S4) => S5, taskA: Task, taskB: Task, taskC: Task, taskD: Task) => Task; /** * Run a side-effect on success. Useful for logging. * @param fn A function will fire with the successful value. * @param task The task to tap on succcess. */ export declare const tap: (fn: (result: S) => void, task: Task) => Task; /** * Run an additional task on success. Useful for async side-effects. * @alias defer * @param fn A function will fire with the successful value. * @param task The task to tap on succcess. */ export declare const tapChain: (fn: (result: S) => Task, task: Task) => Task; /** * Run a function on a successful value which can fail the task or modify the type. * @param fn A function will return a Validation on the value. * @param task The task to tap on succcess. */ export declare const validate: (fn: (value: S) => Validation, task: Task) => Task; /** * Given a task, map the failure error to a Task. * @alias recoverWith * @alias rescue * @param fn A function which takes the original failure error and returns the new one. * @param task The task to map the failure. */ export declare const mapError: (fn: (error: E) => E2, task: Task) => Task; export declare const validateError: (fn: (err: E) => err is E2, task: Task) => Task; export declare const errorUnion: (task: Task) => Task; /** * Given a task, map both the failure error and the success result to a Task. * @param handleError A function which takes the original failure error and returns the new one. * @param handleSuccess A function which takes the original successful result and returns the new one. * @param task The task to map the failure and succeess of. */ export declare const mapBoth: (handleError: (error: E) => E2, handleSuccess: (success: S) => S2, task: Task) => Task; /** * Given a task, map both the failure error and the success result to a Task which always succeeds. * @param handleError A function which takes the original failure error and returns a successful result. * @param handleSuccess A function which takes the original successful result and returns a new successful result. * @param task The task to map failure and succeess to a success for. */ export declare const fold: (handleError: (error: E) => R, handleSuccess: (success: S) => R, task: Task) => Task; /** * Given a task, if the result in a failure, attemp to generate another Task from the error. * @param fn A function which takes the original failure error and returns a Task. * @param task The task to try to run a recovery function on failure. */ export declare const orElse: (fn: (error: E) => Task, task: Task) => Task; /** * Given a task that succeeds with a map function as its result, * run that function over the result of a second successful Task. * @param appliedTask The task whose value will be passed to the map function. * @param task The task who will return a map function as the success result. */ export declare const ap: (task: Task S2>, appliedTask: Task) => Task; /** * Wait some number of seconds to continue after a successful task. * @param ms How long to wait in milliseconds. * @param task Which task to wait to succeed with. */ export declare const wait: (ms: number, task: Task) => Task; /** * If a task fails, retry it in the future. * @param ms How long to wait before trying. * @param task Which task to retry. */ export declare const retryIn: (ms: number, task: Task) => Task; /** * If a task fails, retry it X times, with exponential backoff. * @param ms How long to wait before trying the first time. * @param times How many times to attempt, each waiting 2x the previous time. * @param task Which task to retry. */ export declare const retryWithExponentialBackoff: (ms: number, times: number, task: Task) => Task; /** * Takes a nested task of tasks, which often comes from a map, and * flattens to just the resulting chained task. * @param task The task which resolves to an other task. */ export declare const flatten: (task: Task>) => Task; /** * Given a predicate, if it returns true, error the task with a given value. * @param pred Run this on a successful task, return true to fail the task. * @param error If the predicate succeeded, run this function to get the error result. */ export declare const failIf: (pred: (result: S) => boolean, error: (result: S) => E2, task: Task) => Task; /** * Create a new task. * @param computation A function which will be run when the task starts. */ export declare class Task implements PromiseLike { private computation; static fail: (error: E_1) => Task; static succeed: (result: S_1) => Task; static empty: () => Task; static failIn: (ms: number, error: E_1) => Task; static succeedIn: (ms: number, result: S_1) => Task; static of: (result: S_1) => Task; static all: (tasks: Task[]) => Task; static allSuccesses: (tasks: Task[]) => Task; static sequence: (tasks: Task[], maxConcurrent?: number) => Task; static firstSuccess: (tasks: Task[]) => Task; static never: () => Task; static fromPromise: (maybePromise: S_1 | Promise) => Task; static fromPromises: (promises: Promise[]) => Task; static fromLazyPromise: (getPromise: () => S_1 | Promise) => Task; static wrapPromiseCreator: (fn: (...args: Args) => Promise) => (...args: Args) => Task; static race: (tasks: Task[]) => Task; static external: () => ExternalTask; static emitter: (fn: (...args: Args) => R) => [ExternalTask, (...args: Args) => void]; static succeedBy: (result: () => S_1) => Task; static ap: (task: Task S2>, appliedTask: Task) => Task; static map2: (fn: (a: S_1) => (b: S2) => S3, taskA: Task, taskB: Task) => Task; static map3: (fn: (a: S_1) => (b: S2) => (c: S3) => S4, taskA: Task, taskB: Task, taskC: Task) => Task; static map4: (fn: (a: S_1) => (b: S2) => (c: S3) => (d: S4) => S5, taskA: Task, taskB: Task, taskC: Task, taskD: Task) => Task; static loop: (fn: (currentValue: T) => Task | LoopContinue>, initialValue: T) => Task; static reduce: (fn: (acc: V, currentValue: T, index: number, original: T[]) => Task, initialValue: V, items: T[]) => Task; static zip: (taskA: Task, taskB: Task) => Task; static zipWith: (fn: (resultA: S_1, resultB: S2) => V, taskA: Task, taskB: Task) => Task; static flatten: (task: Task>) => Task; isCanceled: boolean; constructor(computation: Fork); fork(reject: Reject, resolve: Resolve): { cancel: () => void; }; cancel(): void; /** * Alias to match promise API and let async/await work. * Mostly "private". Do not use. */ then(onfulfilled?: ((value: S) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike) | undefined | null): PromiseLike; chain(fn: (result: S) => Task): Task; succeedIf(fn: () => S | undefined): Task; onlyOnce(): Task; toPromise(): Promise; swap(): Task; map(fn: (result: S) => S2): Task; forward(value: S2): Task; append(a: A, b: B, c: C, d: D, e: E): Task; append(a: A, b: B, c: C, d: D): Task; append(a: A, b: B, c: C): Task; append(a: A, b: B): Task; append(a: A): Task; prepend(a: A, b: B, c: C, d: D, e: E): Task; prepend(a: A, b: B, c: C, d: D): Task; prepend(a: A, b: B, c: C): Task; prepend(a: A, b: B): Task; prepend(a: A): Task; tap(fn: (result: S) => void): Task; tapChain(fn: (result: S) => Task): Task; validate(fn: (value: S) => Validation): Task; mapError(fn: (error: E) => E2): Task; validateError(fn: (err: E) => err is E2): Task; errorUnion(): Task; mapBoth(handleError: (error: E) => E2, handleSuccess: (success: S) => S2): Task; fold(handleError: (error: E) => R, handleSuccess: (success: S) => R): Task; orElse(fn: (error: E) => Task): Task; ap unknown ? ReturnType : never>(task: Task): Task; wait(ms: number): Task; retryIn(ms: number): Task; retryWithExponentialBackoff(ms: number, times: number): Task; flatten(this: Task>): Task; failIf(pred: (result: S) => boolean, error: (result: S) => E2): Task; } /** * A special form of Task which can be resolved/rejected externally. */ export declare class ExternalTask extends Task { private computationReject_?; private computationResolve_?; private alreadyError_?; private alreadyResult_?; private lastState_; constructor(); reject(error: E): void; resolve(result: S): void; } declare global { interface Promise { toTask(): Task; } } //# sourceMappingURL=Task.d.ts.map