Callbaq - A callback queue for workflow control.
Lightweight, Zero Dependency.
var Callbaq = require('callbaq');
var workflow = new Callbaq();
// kickoff
workflow.start("new world!");
// add callback functions
workflow.add(function (cbq, input) {
console.log("1: hello world : " + input);
cbq.next("y0y0");
});
workflow.add(function (cbq, input) { console.log("2: here we go : " + input); });
// output:
// 1: hello world : new world!
// 2: here we go : y0y0
Or fire it later, also try using aliases methods
// add callback functions
workflow.add(function (cbq, input) {
console.log("1: hello world : " + input);
cbq.resolve("y0y0");
});
workflow.then(function (cbq, input) {
console.log("2: here we go : " + input);
return "knock knock";
});
// fire
var door = workflow.start("new world!");
// output:
// 1: hello world : new world!
// 2: here we go : y0y0
// content of door: "knock knock"
callbaq is a lightweight callback queue for workflow and callback control with Zero Dependency.
Convert nested callback hell into serial procedural coding style, which is more readable, writable, and maintainable.
Work standalone, or with other modules.
npm i callbaq
Get a new callback queue / flow.
var Callbaq = require('callbaq');
var callbaq = new Callbaq();
Start and walk through the workflow from step 0.
callbaq.start("new world!");
Add (append) a function for use as a callback, which will be executed at the next step.
The parameters for callback are:
callbaq
instance of the current callback queue.- params from
start()
or the output of previous (step) callback function.
callbaq.add(function (cbq, input) {
console.log("something cool : " + input);
cbq.next("a secret");
});
callbaq.then(function (cbq, input) {
console.log("finished with " + input);
});
Check out from the current step and pass result to next step / callback function when works are done.
callbaq.add(function (cbq, input) {
cbq.resolve("A master piece");
});
callbaq.then(function (cbq, input) {
cbq.next("A master piece");
})
The following methods are for EXPERIMENTAL usage and might change in the future.
Avoid using them if possible.
Similar to add(). Returns the queue array.
If called with a function object, the function object will be append into queue.
var old_queue = callbaq.cbq();
var new_queue = callbaq.cbq(function(cbq){ cbq.next("did something") });
Get or set current step number.
Workflow could be changed by calling this method with value.
var current_step_number = cbq.current_step();
// start over from the first step
var new_step_number = cbq.current_step(0);
cbq.next("some params");
Call specific step.
callbaq.step(3, "some params");
The following methods are for EXPERIMENTAL usage and for Expert Only, might change in the future.
Avoid accessing them directly if possible.
The queue of data awaiting for being input of callback functions, Array
of any
.
The callback queue, Array
of function
.
Current step, in number
.
var dataqueue = [1, 2];
// new with default values
var callbaq = new Callbaq({dataqueue: dataqueue});
callbaq.add(function (cbq, input) {
console.log("something in data queue: ", input);
});
// will be executed immediately without needing callbaq.start()
You can have multiple callbaq instances working together or independently.
Just new some and enjoy them.
https://github.com/BlueT/callbaq/blob/master/example.js
Output:
cb1, step 1: hello world : [ 'new world!' ]
v-> cb1, step 2: here we go : [ 'y0y0' ]
| ----------
| cb2, step 1: Are you ok : bro
| ---> inner1: asking bro
| ---> inner2: he's fine
| cb2, step 2: I am very ok : Let's rock N roll
| ----------
^-> cb1, step 3: here we go : [ 'hEy Lo' ]
----------
something in data queue: 1
and guess what! [ 'lala' ]
from dataqueue again! 2
BlueT - Matthew Lien - 練喆明 [email protected]
Waiting for Your Name here.
https://github.com/BlueT/callbaq/issues
You can do a Fork, fix bug or improve code, and send a PR.
Or you can file a issue for bugs or improvements.
If you find this module useful, please give me a Star, I'll be happy whole day.
Hope this module can save your time, a tree, and a kitten.
AnyEvent::CallbackStack - Perl version https://github.com/BlueT/AnyEvent-CallbackStack