task.md

December 15, 2024 ยท View on GitHub

Task

A Task makes it easy to model asynchronous operations that may fail, such as HTTP requests or reading/writing to files/databases.

Implements: Monad, Semigroup

Task(f)

Task constructor.

ParamTypeDescription
ffunctionA function that takes two arguments: reject and resolve, which are functions. The function f normally initiates an asynchronous task or one that has side effects, and once it completes, it either calls the resolve function to resolve the task or rejects it.

Task.of(v)

Task constructor that creates a Task which immediately resolves.

ParamTypeDescription
vanyThe value that is passed to the resolve function

Task.rejected(v)

Task constructor that creates a Task which immediately gets rejected.

ParamTypeDescription
vanyThe value that is passed to the rejected function

Task.fork(reject, resolve)

Executes the Task.

ParamTypeDescription
rejectfunctionFunction to be called when the Task is rejected
resolvefunctionFunction to be called when the Task is resolved

Task.toString()

Gets a stringified version of the Task.

Task.map(f)

Applies the function f to the value of a successfully resolved Task.

ParamTypeDescription
ffunctionFunction

Task.getValue()

Gets the function within the Task.

Task.ap(t)

Applies the successful value of the Task t to the successful value (a function) of the current Task.

ParamTypeDescription
tTaskTask with a function as the second element

Task.concat(t)

Concatenates the current Task with the passed one and returns a new Task. When resolved, it will return the successful result of both tasks.

ParamTypeDescription
tTaskTask to concatenate

Task.chain(f)

Chains together many computations that return a Task.

ParamTypeDescription
ffunctionFunction that returns another Task

Task.toPromise()

Converts the current Task to a Promise.