JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16487
  • Score
    100M100P100Q127329F

Idea for callbacks as values

Package Exports

  • continuable
  • continuable/bind
  • continuable/error
  • continuable/map
  • 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(lambda) { return function (source) {
    return function continuable(callback) {
        source(function (err, value) {
            callback(err, err ? null : lambda(value))
        })
    }
} }

var asString = map(String)
var asJSON = map(function (x) { return JSON.parse(x) })

asJSON(asString(readFile("/tmp/foo.json")))(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(lambda)(source)

map := (lambda:(A) => B) => (source:Continuable<A>) => 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(String)
var asJSON = map(function (x) { return JSON.parse(x) })

var json = asJSON(asString(readFile("/tmp/foo.json")))

json(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(String)
var asJSON = map(function (x) { return JSON.parse(x) })

var json = asJSON(asString(readFile("/tmp/foo.json")))

var write = map(function (json) {
    return function continuable(cb) {
        fs.writeFile("/tmp/bar.json", JSON.stringify(json), cb)
    }
})(json)

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(function (body) {
    if (!body) {
        return error(new Error("Need body"))
    }

    return db.write(body)
})(body)

join(dbWrite)(function (err, writeResult) {
    /* do stuff */
})

bind(lambda)(continuable)

bind :: (A => Continuable<B>) => (Continuable<A>) => Continuable<B>

bind 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 = bind(function (body) {
    if (!body) {
        return error(new Error("Need body"))
    }

    return db.write(body)
})(body)

dbWrite(function (err, writeResult) {
    /* do stuff */
})

Installation

npm install continuable

Contributors

  • Raynos

MIT Licenced