async-fifo.js
Yet another simple fifo for callbacks. After getting annoyed of the various ways ES6 tries to hide the unavoidable asynchronous nature of the event-loop (generator functions, Promises, async/await...), I wrote yet another callback-chainer using node's EventEmitter. At least you understand best what you write yourself...
Installation
As npm for Node.js:
$ npm install async-fifo
Example
Some console interaction:
AsyncFIFO = require("async-fifo");
al = new AsyncFIFO();
al.count = 0; // Use the object to store your data / result
// Enqueue a function with a single argument
al.enqEnd(function(ms) {
setTimeout(() => {
console.log(this.count++);
if (ms < 0)
throw new Error("Done.");
this.enqCurrentAgain(ms - 100);
this.next();
}, ms);
}, 1000);
// ^^^ The argument for the function.
// start
al.on("error", (err) => console.log(err.stack));
al.next();
API
AsyncFIFO()
Initializes a new empty callback chain.
AsyncFIFO#next()
Continues or starts the chain. All the functions you put in the chain must call this at some point.
AsyncFIFO#enq(fn, ...args)
Puts the function provided in fn at the back. Typical FIFO.
AsyncFIFO#enqFront(fn, ...args)
Puts the function provided in fn at the front. Makes a stack (LIFO).
AsyncFIFO#enqCurrentAgain(...different_args) {
This can be used during execution to start the currently executing function again, after the next() call.
AsyncFIFO#error(err)
Stops further execution and triggers an "error" event. Passes err. This is also called internally if an exception happens.
AsyncFIFO#done
Tells if the execution for this pipeline has come to an end. If the cause of the stop was an error, .error_message contains the delivered error. Set to true if you want to stop execution without an error before the queue is empty. Will raise the "done" event.
Testing
As npm package:
$ npm test
Licence
ISC