enQue.js
Chain asynchronous functions in succession, consuming the same data stream.
const Que = new require('enQue');
Que([fn1, fn2, fn3, fn4, fn5]).run(data).then(data=>YOUR_CALLBACK(data));
For full documentation see the enQue full docs.
Features
1.) Skip ahead in the que next(4)
would skip to position 4.
2.) Go backwards in the que next(-4)
would go back 4.
3.) Inject a Function
at a particular que index next({inject:4,function:Function})
. (runs parallel to que not sync'd)
4.) Quit after a specified amount of que spots next({quit:4})
quits after 4 more iterations.
5.) Allows removing functions from the que by index, variable name, or raw text.
6.) A fill method for convenience.
The above code snippet illustrates how simple it would be to execute 5 asynchronous functions in succession that all operate on the same data. When you add a function to the que accept the second a parameter (data is first) and make sure to call it for example FUNCTION_NAME(data, next) { /* example: */ data.size + 1; /* MANDATORY */ next() }
. You can also use next(0)
to quit early. The entire point of this package is that each spot in the que wont run untill the last spot has said ok im done via calling next()
, or whatever you chose to name it.
Since a promise is returned it is always best to attach a .then()
and .catch()
so the above code would become.
myQue.run(data)
.then(sucessCallback);
.catch(errorCallback);
Instalation
npm install enque
Dependencies
None.
Usage examples
const Que = require('enQue');
const que = new Que();
var once = true;
que.add((data, next, done, index) => {
setTimeout(()=>{
data.msg += ' ONE';
next()
}, 9000)
});
que.add((data, next) => {
setTimeout(()=>{
data.msg += ' TWO';
next();
}, 4000)
});
que.add((data, next) => {
data.msg += ' THREE';
next()
});
function myFn1(data, next) {
data.msg += ' FOUR';
if(once) { once = false; next(-2); }
else next(1);
}
function myFn2(data, next) {
data.msg += ' FIVE';
next();
}
que.add([myFn1, myFn2])
que.run({msg: 'ZERO'})
.then(res => console.log(res.msg))
.catch(err => console.log('Woopsie! ' + err))
Executing the above code as is gives:
ONE TWO THREE FOUR TWO THREE FOUR FIVE
More examples
NOTE You can't actually call que.run()
then que.clear()
because the que may still be processing. You need to call. que.run().then(()=>que.clear())
or to extract data from the processed que que.run().then(data=>YOUR_CALLBACK(data))
. Each function in your que is handed data as a first parameter whether or not you originally passed one in. default {}
Que = require('enQue');
que = new Que();
function fn1(data, next) {
console.log(1);
next();
}
function fn2(data, next) {
console.log(2);
next();
}
que.add(fn1);
que.run();
que.clear();
que.add([fn1, fn1, fn1]);
que.run();
que.clear();
que.fill(fn1, 7);
que.run();
que.clear();
que.add([fn1, fn1, fn1]);
que.remove(fn1);
que.run();
que.add([fn1, fn2, fn2, fn2]);
que.remove([fn1, fn2], 3);
que.run();
que.clear();
que.add([(d,n)=>n(5), fn1, fn1, fn1, fn1, fn1, fn1]);
que.run();
que.clear();
que.add([(d,n)=>n({quit:3}), fn1, fn1, fn1, fn2, fn2, fn2]);
que.run();
que.clear();
que.add((d,n,i)=>{console.log("hi"); n()});
que.remove('(d,n,i)=>{console.log("hi"); n()}');
que.run();
fn5 = (data, next, index, done) => {
console.log(index);
index === 2 ? next(0) : next();
}
que.add([fn5, fn5, fn5, fn5, fn5]);
que.run();
que.clear();
fn3 = (data, next) => {
data.data = "SEVEN";
next();
}
que.add([(d,n)=>n({inject:5, function: function(d){d.data="7"}}), fn3, fn3, fn3, fn3, fn3]);
que.run().then(res=>console.log(res));
que.clear();
function fn6(data, next, index) {
throw new Error("Woopsie!");
next();
}
que.add([fn6, fn6, fn6, fn6, fn6]);
que.run().catch(e=>console.log(e));
Executing the above code as is gives:
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
0
1
2
{ data: '7' }
Error: Woopsie!
at fn6 (...)
at options (...)
at enQue.executeQue (...)
at ...
...