Task
Index
Constructor
create
- const getTime = Task(() => Task.ok(Date.now()));const fetchTask = (url: string) => Task(() => fetch(url).then(Task.ok, Task.error));const delay = (ms: number) => Task(() => new Promise(resolve => { setTimeout(() => resolve(Task.ok()); }), ms));
reject
Constructor that always returns a failed
Taskthat rejectsvoid.const task = Task.reject();const result = Task.run(task);// Result.Error()
resolve
Constructor that always returns a successful
Taskthat resolvesvoid.const task = Task.resolve();const result = Task.run(task);// Result.Ok()
Type
hasInstance
Return
trueif anyValue is a validTaskTask.hasInstance(Task.resolve(...)); // trueTask.hasInstance({}); // false
Other
ErrorOf
Extracts error type of task T
Type parameters
- T
ValueOf
Extracts value type of task T
Type parameters
- T
error
ok
all
Resolves with the array of all task values, or reject with the first error
const success = Task.all([Task.resolve(1),Task.resolve(2),]);const successResult = Task.run(success);// Result.Ok([1, 2])const failure = Task.all([Task.resolve(1),Task.reject('error'),]);const failureResult = Task.run(failure);// Result.Error('error')
allKeyed
Resolves with the record of all task values, or reject with the first error
const success = Task.allKeyed({task1: Task.resolve(1),task2: Task.resolve(2),});const successResult = Task.run(success);// Result.Ok({ task1: 1, task2: 2 })const failure = Task.allKeyed({task1: Task.resolve(1),task2: Task.reject('error'),});const failureResult = Task.run(failure);// Result.Error('error')
allSettled
Resolves an array of all task results
const task = Task.allSettled([Task.reject(1),Task.resolve(2),]);const taskResults = Task.run(task);// [Result.Error(1), Result.Ok(2)]
allSettledKeyed
Resolves with the record of all task values, or reject with the first error
const success = Task.allSettledKeyed({task1: Task.resolve(1),task2: Task.reject('error'),});const result = Task.run(success);// Result.Ok({task1: Result.Ok(1),task2: Result.Error('error'),})
andRun
Similar to andThen but the task keep
taskresolved valueconst success = Task.resolve('foo');Task.andRun(success, (value) => Console.log('result=', value));// console.log('result=foo'); then resolves 'foo'Task.andRun(success, (value) => Task.reject(`SomeError`));// Task.reject('SomeError')const failure = Task.reject('PreviousError');Task.andRun(failure, (value) => Task.resolve(`never_used`));// Task.reject('PreviousError')
andThen
Calls
fnif the task is successful, otherwise returns the failed task untouched. This function can be used for control flow based onTaskvalues.const success = Task.resolve('foo');Task.andThen(success, (value) => Task.resolve(`${value}_then`));// Task.resolve('foo_then')const failure = Task.reject('PreviousError');Task.andThen(failure, (value) => Task.resolve(`never_used`));// Task.reject('PreviousError')
any
Resolves with the first value, or reject with an aggregated error
const success = Task.any([Task.reject(1),Task.resolve(2),]);const successResult = Task.run(success);// Result.Ok(2)const failure = Task.any([Task.reject('error1'),Task.reject('error2'),]);const failureResult = Task.run(failure);// Result.Error(AggregateError({ errors: ['error1', 'error2']}))
from
Create a Task from a
Symbol.runfunction or a TaskLikeconst task = Task.from(({ resolve }) => resolve('hello'));// from a callbackconst task = Task.from({ [Symbol.run]: ({ resolve }) => resolve('hello') });// from a TaskLike
ignore
Ignores value of task
const task = Task.resolve('foo');Task.ignore(task);// Task.resolve()
map
Maps a
Task<Value, Error>toTask<NewValue, Error>by applying a function to a success value, leaving a failure untouched. This function can be used to compose the results of two functions.const task = Task.resolve('foo');Task.map(task, (value) => `${value}_bar`));// Task.resolve('foo_bar')
mapError
Maps a
Task<Value, ErrorFrom>toTask<Value, ErrorTo>by applying a function to a contained failure error, leaving a success value untouched. This function can be used to pass through a successful result while handling an error.const task = Task.reject('error');Task.mapError(task, (value) => `${value}_bar`));// Task.reject('error_bar')
mapResult
Maps a
Task<ValueFrom, ErrorFrom>toTask<ValueTo, ErrorTo>by applying a function to the result of the task.const task = Task.reject('error');const handledTask = Task.mapResult(task, (result) =>Result.isOk(result) ? result : Result.Ok('handled_value') )); // Task.resolve('handled_value')
orElse
Calls
fnif the task is failed, otherwise returns the successful task untouched. This function can be used for control flow based onTaskvalues.const success = Task.resolve('foo');Task.orElse(success, (value) => Task.resolve(`never_used`));// Task.resolve('foo')const failure = Task.reject('PreviousError');Task.orElse(failure, (error) => Task.reject(`${error}_caught`));// Task.reject('PreviousError_caught')
run
Run
taskand return the result or a promise of the result⚠ Impure function that may throw an error, it should be used on the edge of the program.
const getMessage = Task.resolve('Hello World!');const messageResult = Task.run(getMessage);// Result.Ok('Hello World!')
tryCall
Creates a new
Taskthat resolvessideEffect(). When an exception is thrown then it rejectsonError([thrown error]).const class ResponseError extends Error {}const fetch = Task.tryCall(() => fetch('my/url'), // Task will resolve Ok(fetch('my/url'))(error) => new ResponseError(), // Task will reject Error(new ResponseError()));// Task<Response, ResponseError>const randomNumber = Task.tryCall(async () => Math.random());// Task<number, never>
Task constructor