chainit
Turn an asynchronous JavaScript api into an asynchronous
chainable JavaScript api.
Features:
- supports async apis
- supports nested calls
- preserve nested calls order
- preserve context in callbacks
- preserve callback arguments
usage
function MyApi() {
}
MyApi.prototype.method1 = function(cb) {
}
MyApi.prototype.method2 = function(cb) {
}
var chainit = require('chainit');
var MyChainApi = chainit(MyApi);
var obj = new MyChainApi();
obj
.method1()
.method2()
.method1(function() {
this.method1();
})
.method2();
Call order:
- method1
- method2
- method1
- method1
- method2
examples
See examples.
tests
See tests.
npm test
async/sync apis
There is no easy way to mix sync/async chainable
apis because there is no way to differenciate sync/async calls.
obj
.asyncMethod()
.syncMethod()
We cannot know that syncMethod is synchronous and that
we do not
need to wait for a callback to be called to continue.
Either your api is fully asynchronous and every method
takes a callback.
Either your api is fully synchronous.
If you want synchronous support, make a pull request
adding chainit.sync(Constructor)
.
credits
This module was done easily thanks to
jessetane/queue.
A chainable api is just queueing methods and reordering calls.
This module was built to replace the chainable api from
webdriverjs.