Package Exports
- continuable
- continuable/chain
- continuable/error
- continuable/map
- continuable/maybe-callback
- continuable/of
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (continuable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
continuable
Idea for callbacks as values
Example
var readFile = function (uri) {
return function (cb) {
fs.readFile(uri, cb)
}
}
function map(source, lambda) {
return function continuable(callback) {
source(function (err, value) {
callback(err, err ? null : lambda(value))
})
}
}
var asString = map(readFile("/tmp/foo.json"), String)
var asJSON = map(asString, function (x) { return JSON.parse(x) })
asJSON(function (err, value) {
/* do stuff with JSON */
})
Docs
Continuable(callback)
type Continuable := (callback:(Error, Value) => 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 readFile = function (uri) {
return function continuable(callback) {
fs.readFile(uri, callback)
}
}
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
map(source, lambda)
map := (source:Continuable<A>, lambda:(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 = map(readFile("/tmp/foo.json"), String)
var asJSON = map(asString, function (x) { return JSON.parse(x) })
asJSON(function (err, json) {
/* do stuff */
})
join(continuable)
join := (source:Continuable<Continuable<T>>) => 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 = map(readFile("/tmp/foo.json"), String)
var asJSON = map(asString, function (x) { return JSON.parse(x) })
var write = map(asJSON, function (json) {
return function continuable(cb) {
fs.writeFile("/tmp/bar.json", JSON.stringify(json), cb)
}
})
join(write)(function (err, writeResult) {
/* stuff */
})
of(value)
of := (Value) => 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.
function getThing() {
var thing = localStorage.getItem("thing")
if (thing) return of(thing)
return ajax("/thing")
}
error(err)
error := (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 = getBody(req, res)
var dbWrite = map(body, function (body) {
if (!body) {
return error(new Error("Need body"))
}
return db.write(body)
})
join(dbWrite)(function (err, writeResult) {
/* do stuff */
})
chain(continuable, lambda)
chain := (Continuable<A>, (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 = getBody(req, res)
var dbWrite = chain(body, function (body) {
if (!body) {
return error(new Error("Need body"))
}
return db.write(body)
})
dbWrite(function (err, writeResult) {
/* do stuff */
})
either(continuable, left, right?)
either := (source: Continuable<A>,
left: (Error) => Continuable<B>,
right?: (A) => 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 = require("fs")
var either = require("continuable/either")
var fileStat = fs.stat.bind(null, "./package.json")
var fileExists = either(fileStat, function left(err) {
return fs.writeFile.bind(null, "./package.json", "{}")
}) // note the right function is optional
var file = chain(fileExists, function () {
return fs.readFile.bind(null, "./package.json")
})
file(function (err, body) {
// There is no error because we create an empty file if the
// stat failed. Body is either body or {}
})
Installation
npm install continuable
Contributors
- Raynos