https://github.com/bluet/callbaq
Convert nested callback hell into serial procedural coding style, which is more readable, writable, and maintainable.
https://github.com/bluet/callbaq
async callback callback-functions callback-queue fossa-status hacktoberfest procedural promise queue workflow
Last synced: 2 months ago
JSON representation
Convert nested callback hell into serial procedural coding style, which is more readable, writable, and maintainable.
- Host: GitHub
- URL: https://github.com/bluet/callbaq
- Owner: bluet
- License: mit
- Created: 2018-05-11T03:50:58.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2025-11-28T05:04:06.000Z (5 months ago)
- Last Synced: 2025-11-28T17:12:15.818Z (5 months ago)
- Topics: async, callback, callback-functions, callback-queue, fossa-status, hacktoberfest, procedural, promise, queue, workflow
- Language: JavaScript
- Homepage:
- Size: 267 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Callbaq
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fbluet%2Fcallbaq?ref=badge_shield)
## NAME
**Callbaq** - A callback queue for workflow control.
Lightweight, **Zero Dependency.**
## SYNOPSIS
```javascript
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
```javascript
// 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"
```
## DESCRIPTION
**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.
## INSTALLATION
`npm i callbaq`
## METHODS
### **new**
Get a new callback queue / flow.
```javascript
var Callbaq = require('callbaq');
var callbaq = new Callbaq();
```
### **start**
Start and walk through the workflow from step 0.
```javascript
callbaq.start("new world!");
```
### **add / then**
Add (append) a function for use as a callback, which will be executed at the next step.
The parameters for callback are:
1. `callbaq` instance of the current callback queue.
2. params from `start()` or the output of previous (step) callback function.
```javascript
callbaq.add(function (cbq, input) {
console.log("something cool : " + input);
cbq.next("a secret");
});
callbaq.then(function (cbq, input) {
console.log("finished with " + input);
});
```
### **next / resolve**
Check out from the current step and pass result to next step / callback function when works are done.
```javascript
callbaq.add(function (cbq, input) {
cbq.resolve("A master piece");
});
callbaq.then(function (cbq, input) {
cbq.next("A master piece");
})
```
## EXPERIMENTAL use of Methods
The following methods are for EXPERIMENTAL usage and might change in the future.
Avoid using them if possible.
### cbq
Similar to add(). Returns the queue array.
If called with a function object, the function object will be append into queue.
```javascript
var old_queue = callbaq.cbq();
var new_queue = callbaq.cbq(function(cbq){ cbq.next("did something") });
```
### current_step
Get or set current step number.
Workflow could be changed by calling this method with value.
```javascript
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");
```
### step
Call specific step.
```javascript
callbaq.step(3, "some params");
```
## ATTRIBUTES
The following methods are for EXPERIMENTAL usage and for Expert Only, might change in the future.
Avoid accessing them directly if possible.
### dataqueue
The queue of data awaiting for being input of callback functions, `Array` of `any`.
### _cbq
The callback queue, `Array` of `function`.
### _current_step
Current step, in `number`.
### Create with custom initial values
```javascript
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()
```
## EXAMPLES
You can have multiple callbaq instances working together or independently.
Just new some and enjoy them.
```
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
```
## AUTHOR
BlueT - Matthew Lien - ็ทดๅๆ
## CREDITS
Waiting for **Your Name** here.
## BUGS
https://github.com/BlueT/callbaq/issues
## CONTRIBUTE
You can do a [Fork](https://github.com/BlueT/callbaq#fork-destination-box), fix bug or improve code, and send a PR.
Or you can [file a issue](https://github.com/BlueT/callbaq/issues) 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.
## SEE ALSO
AnyEvent::CallbackStack - Perl version https://github.com/BlueT/AnyEvent-CallbackStack
## License
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fbluet%2Fcallbaq?ref=badge_large)