Ensures a sequential execution of functions, helps you escape callback hell, comes in handy when writing long and complex tasks. Works exactly as expressjs middlewares.
$ npm install execution-line
Browser
<script type="text/javascript" src="/path/to/execution-line.min.js"></script>
ExecutionLine(context, myObject)
Name | Type | Required | Description |
---|---|---|---|
context | Object | Optional | thisArg for all functions on the line |
myObject | Object | Optional | a custom object to use across all functions |
You must always use an instance.
let line = new ExecutionLine();
Hook functions to the line.
Name | Type | Required | Description |
---|---|---|---|
fn | Function or Array | Required | if a Array is passed it must be an Array of Functions, otherwise it will throw TypeError |
line.hook([
function(next){ /* step 1 */ next(); },
function(){ /* step 2 */ }
]);
Executes all hooked functions. Each function is removed from the line to be executed, if all functions were executed the line will be clear again. If exec()
is called and the line is clear it will return false
.
Clear line, removes all hooked functions.
Executed after all functions, it shares the same context as your other functions. Only works if the last function calls next()
.
Name | Type | Description |
---|---|---|
myObject | Object or Null | a custom object to use across all functions |
line.onexecutionend = function(myObject) {}
Basic usage
// only needed on nodejs
let ExecutionLine = require('execution-line');
let lineA = new ExecutionLine();
function firstA(next) {
console.log('line A - first function');
next();
}
function secondA() {
console.log('line A - second function')
}
lineA.hook(firstA);
lineA.hook(secondA);
let lineB = new ExecutionLine();
function firstB(next) {
console.log('line B - first function');
next();
}
function secondB(){
console.log('line B - second function')
}
lineB.hook(firstB)
lineB.hook(secondB);
lineA.exec();
lineB.exec();
// outputs
// line A - first function
// line A - second function
// line B - first function
// line B - second function
Changing context, thisArg
// only needed on nodejs
let ExecutionLine = require('execution-line');
let line = new ExecutionLine({ e: 'element'});
line.hook(function(next){
console.log(this.e);
});
line.exec();
// outputs
// element
Passing a object to functions
// only needed on nodejs
let ExecutionLine = require('execution-line');
let line = new ExecutionLine({}, { prop: 'string'});
function a(obj, next){
console.log(obj.prop);
next();
}
line.hook(a);
line.hook(a);
line.exec();
// outputs
// string
// string
Using onexecutionend
// only needed on nodejs
let ExecutionLine = require('execution-line');
let line = new ExecutionLine();
line.hook(function(next){
console.log('step 1');
// not calling next()
});
line.onexecutionend = function() {
console.log('final step');
}
line.exec();
// outputs
// step 1
// hooking again because after execution the line is clear
line.hook(function(next) {
console.log('step 1');
// calling next()
next();
});
// outputs
// step 1
// final step
Escaping callback hell, silly example using callback:
element.onclick(function(event){
event.target.disabled = true;
xmlhttp.onload = function() {
some.SDK.call(function() {
anotherXmlHttp.onload = function() {
event.target.disabled = false;
// now its over
}
anotherXmlHttp.send();
});
}
xmlhttp.send()
});
Same example using ExecutionLine:
function clickHandler(next) {
this.disabled = true;
next();
}
function ajaxCall(next) {
xmlhttp.onload = function() {
next();
}
xmlhttp.send();
}
function doSomething(next) {
some.SDK.call(function(){
next();
});
}
function lastAjaxCall() {
xmlhttp.onload = function() {
this.disabled = false;
}
xmlhttp.send();
}
element.onclick = function() {
let line = new ExecutionLine(element);
line.hook(clickHandler);
line.hook(ajaxCall);
line.hook(doSomething);
line.hook(lastAjaxCall);
line.exec();
}