continuable
Idea for callbacks as values
Example
var { return { fs }} { return { }} var asString = var asJSON =
Docs
Continuable(callback)
type Continuable := void void
A continuable is simply a function that takes a single argument, a callback. The callback get's called with the normal node error and value pattern.
// readFile := (String) => Continuable<Buffer>var { return { fs }}
The reason to have a continuable instead of passing a callback directly into another value is that a continuable is a concrete value that can be returned.
Which means you can call useful functions on this value like map
and join
to(asyncFunction)
take an async function and return a function that works as async function or continuable.
var readFile = continuable{//there you go}
map(source, lambda)
map := source:Continuable<A> B Continuable<B>
map takes a transformation function and a continuable and returns a new continuable. The new continuable is the value of the first continuable transformed by your mapping function.
var asString = var asJSON =
mapAsync(source, lambda)
mapAsync := (source: Continuable<A>, lambda: (A, Callback<B>)) => Continuable<B>
mapAsync takes an asynchronous transformation function and a source continuable. The new continuable is the value of the first continuable passed through the async transformation.
var asString = var asJSON = var written =
join(continuable)
join := Continuable<T>
join
takes a continuable that contains another continuable and flattens it by
one layer. This is useful if you return another asynchronous operation from
map
var asString = var asJSON = var write = { /* stuff */}
both(source)
continuable := (Continuable<A>) => Continuable<[Error, A]>
both
takes a continuable and returns a continuable containing a tuple of
the error and the value. The returned continuable will never contain an
error.
This is useful for handling errors using if statements
var { var source = fsreadFile var maybeFile = return }
of(value)
of := Continuable<Value>
of
takes any value and returns a Continuable for this value. This is useful
if you want to implement a function that either returns a value or a
continuable.
{ var thing = localStorage if thing return ofthing return }
error(err)
error := Continuable<void>
error
takes any error and returns a Continuable that will return said error.
This is useful if you want to transform a normal continuable into an
error state one.
var body = var dbWrite = { /* do stuff */}
chain(continuable, lambda)
chain := Continuable<A> Continuable<B> Continuable<B>
chain
takes a lambda function that is given the value and returns another
continuables. The result will be a continuable given the value of the
returned continuable.
In combination with of
this makes Continuable
a monad.
Alternatively this can be seen as sugar for map
followed by join
var body = var dbWrite =
either(continuable, left, right?)
either := source: Continuable<A> Continuable<B> right?: Continuable<B> Continuable<B>
either
takes a source continuable and a left and right function.
It will either call the left function with the error in source
or call the right function with the value in the source.
The returned continuable will contain the value returned from either left or right. Note that left and right return continuables themself.
var fs = var either = var fileStat = fsstatvar fileExists = // note the right function is optional var file =
The left function can either return a Continuable or call the passed callback directly. For example:
var item = fsstatvar maybeItem =
Using the callback form is convenient and avoids the usage of
return return of(null)
and return error(err)
series([continuables])
Given an array of continuables return a continuable that invokes them in order, or until one errors.
para([continuables])
See continuable-para
Given an array on continuables return a continuable
Installation
npm install continuable
Contributors
- Raynos