execution-line
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.
Installation
$ npm install execution-line
Browser
API
Constructor
Parameters
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 |
Usage
You must always use an instance.
let line = ;
line.hook(fn)
Hook functions to the line.
Parameters
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 |
Usage
line;
line.exec()
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
.
line.clear()
Clear line, removes all hooked functions.
line.onexecutionend
Executed after all functions, it shares the same context as your other functions. Only works if the last function calls next()
.
Parameters
Name | Type | Description |
---|---|---|
myObject | Object or Null | a custom object to use across all functions |
Usage
line {}
Usage
Example 1
Basic usage
// only needed on nodejslet ExecutionLine = ; let lineA = ; { console; ;} { console} lineA;lineA; let lineB = ; { console; ;} { console} lineBlineB; lineA;lineB; // outputs// line A - first function// line A - second function// line B - first function// line B - second function
Example 2
Changing context, thisArg
// only needed on nodejslet ExecutionLine = ; let line = e: 'element';line; line; // outputs// element
Example 3
Passing a object to functions
// only needed on nodejslet ExecutionLine = ; let line = {} prop: 'string';{ console; ;}line;line; line; // outputs// string// string
Example 4
Using onexecutionend
// only needed on nodejslet ExecutionLine = ; let line = ;line; line { console;} line; // outputs// step 1 // hooking again because after execution the line is clearline; // outputs// step 1// final step
Example 5
Escaping callback hell, silly example using callback:
element;
Same example using ExecutionLine:
{ thisdisabled = true; ;} { xmlhttp { ; } xmlhttp;} { someSDK;} { xmlhttp { thisdisabled = false; } xmlhttp;} element { let line = element; line; line; line; line; line;}