Javascript Chain
This is a small javascript function to rescue you from infinite callbacks.
Install
npm install jschain
Quick Start
var _map = { console; ; } { console; ; }; _map;
When to use it?##
Javascript callbacks may looks like this:
;
The traditional callback style is no longer suitable for heavy event driven style programming.
Now, see what JSChain can do:
{ console; ; } { console; ; };
With the help of JSChain, the first demo code could be written like this:
var myController = { ... ... ; ... } { ... ... ; } { ... ; } { ... ; ... }; myController;
And JSChain is very useful for data scraping projects:
{ console; ;} var chain = getURL: getURL;forvar i=1;i<100;i++ chain;
The JSChain itself is very small. The full documented source code of JSChain is only 1.6KB. I can paste the source code here, so you can read the source code. This could help you understand how it works.
{ var self = this; this____items = ; //this remembers the callback functions list this____finally = null; this //execute next function { if !jump var func = self____items; if func func; else self____items = ; if self____itemslength == 0 && typeof self____finally == 'function' self____finally; self____finally = null; }; this //support for custom function { var args = slicefunc = arguments0; args; self____items; return self; }; this //support for final function { self____finally = func; return self; }; //copy and wrap the methods of obj forvar func in obj if typeof objfunc == 'function' && obj { selffunc = { var args = slice; args; //pass next callback to the last argument self____items; return self; //always return JSChain it self like jQuery } }func; // this is the closure tricks //start execute the chained functions in next tick ; return this;}
Advanced features##
end method###
An end
method can add a function to the end of the chain. You can not define this in the object when new JSChian().
jump to the end instead of going to next###
When you call next
method, if you pass a true
value into next()
method, that means JUMP TO THE END.
data passing along functions###
var obj =
{
a: function(i,next)
{
this.A = i.toUpperCase();
next();
},
b: function(next)
{
this.B = this.A+'B';
next();
}
};
var c = new JSChain(obj);
c.a('a')
.b()
.exec(function()
{
console.log(this);
});
this will output:
{ a: [Function], b: [Function], A: 'A', B: 'AB' }