Module: control.async

Stability:1 - Experimental
Bug Tracker:https://github.com/folktale/control.async
Version:0.5.1
Repository:https://github.com/folktale/control.async
Portability:Portable
npm package:control.async
Task(_, _) → AsyncModule

Operations for asynchronous control flow.

Loading

Require the control.async package, after installing it, and give it a valid data.task.Task object to instantiate it:

1
2
var Task = require('data.task')
var Async = require('control.async')(Task)

Combining tasks

parallel()

control.async.parallel(tasks)
Returns:A task that runs the given ones in parallel.
Array(Task(α, β)) → Task(α, Array(β))

Resolves all tasks in parallel, and collects their results.

nondeterministicChoice()

control.async.nondeterministicChoice(tasks)
Returns:A task that selects the first task to resolve.
Array(Task(α, β)) → Task(α, Maybe(β))

Runs all tasks in parallel, selects the first one to either succeed or fail.

choice()

control.async.choice(tasks)
Array(Task(α, β)) → Task(α, Maybe(β))

Alias for nondeterministicChoice()

tryAll()

control.async.tryAll(tasks)
Array(Task(α, β)) → Task(Array(α), Maybe(β))

Creates a task that succeeds if one task succeeds, or fails if all of them fail.

Converting

lift()

control.async.lift(function)
(α₁, α₂, ..., αₙ, (β → Unit)) → (α₁, α₂, ..., αₙ → Task(Unit, β))

Converts a function that takes a simple continuation to a Task.

liftNode()

control.async.liftNode(function)
(α₁, α₂, ..., αₙ, (β, γ → Unit)) → (α₁, α₂, ..., αₙ → Task(β, γ))

Converts a function that takes a Node-style continuation to a Task.

toNode()

control.async.toNode(task)
Task(α, β) → (α | null, β | null → Unit)

Converts a Task to a Node-style function.

fromPromise()

control.async.fromPromise(promise)
Promise(α, β) → Task(α, β)

Converts a Promises/A+ to a Task.

toPromise()

control.async.toPromise(constructor, task)
PromiseConstructor → Task(α, β) → Promise(α, β)

type PromiseConstructor = new((α → Unit), (β → Unit) → Unit)
                        → Promise(α, β)

Converts from Task to Promises/A+.

Note

Do note that nested Tasks, unlike Promises/A+, are NOT flattened. You need to manually call control.monads.join() until you get to the value itself, if you care about passing just the value.

Error handling

catchOnly()

control.async.catchOnly(filter, task)
(γ → Boolean) → Task(α, β) :: throws(γ) → Task(α | γ, β)

Reifies some errors thrown by the computation to a rejected task.

catchAllPossibleErrors()

control.async.catchAllPossibleErrors(task)
Task(α, β) :: throws(Any) → Task(Any, β)

Reifies all errors thrown by the computation to a rejected task.

Timers

delay()

control.async.delay(milliseconds)
Returns:A Task that succeeds after N milliseconds.
Number → Task(Unit, Number)

Constructs a Task that always succeeds after at least N milliseconds. The value of the Task will be the delta from the time of its initial execution to the time it gets resolved.

timeout()

control.async.timeout(milliseconds)
Returns:A Task that always fails after N milliseconds.
Number → Task(TimeoutError, Unit)

Constructs a Task that always fails after at least N milliseconds.

Transforming

memoise()

control.async.memoise(task)
Task(α, β) → Task(α, β)

Caches the result of a Task, to avoid running the same task again for idempotent or pure tasks.