Skip to content

Latest commit

 

History

History
247 lines (139 loc) · 6.6 KB

File metadata and controls

247 lines (139 loc) · 6.6 KB

Task

The Task[a, b] structure represents values that depend on time. This allows one to model time-based effects explicitly, such that one can have full knowledge of when they're dealing with delayed computations, latency, or anything that can not be computed immediately.

A common use for this structure is to replace the usual Continuation-Passing Style form of programming or Promises (if you prefer not to have your error catching and rejection values handled similarly), in order to be able to compose and sequence time-dependent effects using the generic and powerful monadic operations.

Signature: ((a → b) → c → void) → Task[a, b]

Parameters

run

Run the computation originally passed when creating the Task.

Parameters

Returns Function cancellation

map

Transforms the success value of the Task[_, a] using a regular unary function.

Exposed as both a static function and a method on the Task prototype.

Signature: ((a → b) → Task[, a]) → Task[, b]

Parameters

Returns Task

bimap

Transforms the fail or success values of the Task[a, b] using two regular unary functions depending on what exists.

Exposed as both a static function and a method on the Task prototype.

Signature: ((a → b), (c → d), Task[a, c]) → Task[b, d]

Parameters

Returns Task

chain

Transforms the success value of the Task[a, b] using a function to a monad.

Exposed as both a static function and a method on the Task prototype.

Signature: ((b → Task[c, d]) → @Task[a, b]) → Task[c, d]

Parameters

Returns Task

bichain

Passes both the fail and success values of the Task[a, b] to a function that returns an Task[d, e].

Exposed as both a static function and a method on the Task prototype.

Signature: (a → Task[d, e]) → (b → Task[d, e]) → Task[d, e]

Parameters

Returns Task

ap

Applys the success value of the Task[_, (b → c)] to the success value of the Task[d, b]. Fails with the first failure and throws the second away if it occurs.

Exposed as both a static function and a method on the Task prototype.

Signature: (Task[d, b] → Task[, (b → c)]) → Task[, c]

Parameters

Returns Task

concat

Take the earlier of the two Tasks

Exposed as both a static function and a method on the Task prototype.

Signature: (Task[a, b] → Task[a → b)]) → Task[a, b]

Parameters

Returns Task

cache

Caches the fail and success values from an Task[a, b]. Run can be called multiple times on the produced Task and the computation is not re-run.

Exposed as both a static function and a method on the Task prototype.

Signature: Task[a, b] → Task[a, b]

Returns Task

of

Constructs a new Task[_, b] containing the single success value b.

b can be any value, including null, undefined, or another Task[a, b] structure.

Exposed as both a static function and a method on the Task prototype.

Signature: b → Task[_, b]

Parameters

  • success Any

Returns Task

fail

Constructs a new Task[a, _] containing the single fail value a.

a can be any value, including null, undefined, or another Task[a, b] structure.

Exposed as both a static function and a method on the Task prototype.

Signature: a → Task[a, _]

Parameters

  • fail Any

Returns Task

empty

Returns an Task that will never resolve

Exposed as both a static function and a method on the Task prototype.

Signature: Void → Task[_, _]

Returns Task

fork

Alias for run.

callback

Callback style run which passes fail and success as the first and second arguments. Use of this function is not advised as fail values are allowed to be null though you probably shouldn't be indicating a fail with null :)

Signature: (a, b) → (_ → void)

Parameters

Returns Function cancel

orElse

Creates a task that sends a success if it receives a fail and passes on the value if it receives a success.

Signature: a → Task[_, a]

Parameters

  • else Any
  • elseValue

Returns Task

TaskMaker

Exported Factory function for Tasks. Also includes many utility methods.

Parameters

Returns Task

after

Creates a Task that sends a success after x milliseconds

Parameters

Returns Task

all

Creates a single Task out of many that doesnt complete until each resolve with all successs or a single fail occurs. Will pass the incomplete array of successs if some have occured before a fail.

Signature: [Task[a, b]] → Task[a, [b]]

Parameters

Returns Task