Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including api-chain with all npm packages installed. Try it out:

var apiChain = require("api-chain")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

api-chain v0.0.8

A light and easy to use interface for creating fluent, chainable javascript APIs in Node.js or PhantomJs


When performing many asynchronous operations in javascript, nested callbacks can become difficult to read and maintain. Api-chain is a very light and easy to use interface for creating a fluent synchronous style API for control flow of asynchronous javascript. It is packaged as a commonJS module through NPM but not only works with Node.js, but has been tested and works with PhantomJS as well.

Methods can pass values down the chain by invoking next with them, e.g. next(null, value). Node.js callback style methods will be wrapped automatically so as to pass the results down the chain.


npm install api-chain


// require api-chain module
var api = require('api-chain');

// define your api by passing methods to create
var fs = api.create({
    read: require('fs').readFile,
    toString: function (data, next) {
        next(null, data.toString());
    view: function (contents) {

// example using the chainable 'fs' API as created above'./index.js')

For more examples look in the examples subdirectory.


onErrorfnundefinedcalled when error is emitted unless error method is overwritten
throwErrorsbooltruewhether to throw unhandled errors (ignored if onError exists)
continueErrorsboolfalsewhether to resume execution of commands after errors occur

built in chainable methods

api.wait(n) - pause execution flow for n milliseconds

api.until(fn) - wait until callback fn returns true before continuing execution flow

api.chain(fn) - add callback fn to be executed next in the control flow stack.

  • callback signature: fn([arg1,] [arg2,] ... [arg n,] [next])

api.set(name, value, [immediate]) - set api object property name to value

  • if the immediate flag is set, the value will be set immediately rather than within the control flow

immediate methods

The following methods execute immediately (rather than within the control flow) and always return the api object

api.setOption(option, value) - set option to value

api.setOptions(options) - set several options at once based on options collection of key/value pairs


to test you will need the dev dependencies installed. Just cd to the directory api-chain is installed in and type

npm install

when installation of the dependencies completes, type

npm test


MIT Style License - see license.txt


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free