Chainable logic built on named (or not) queues in Javascript
Tired of this?
stuck(arg, function(err, result) {
if(err) return console.log(err);
In(result, function(err, result) {
if(err) return console.log(err);
callback(result, function(err, result) {
if(err) return console.log(err);
hell(result);
});
});
});
("stuck in callback hell")
What if you could write it like this?
q([arg])
.node(notStuck, 'error')
.node(In, 'error')
.node(callback, 'error')
.each(hell);
q('error')
.each(console.log);
Tired of this?
async.waterfall([
dealing.bind(this, arg),
function With(result, callback) {
if(result.foo == 'branch1') {
async.waterfall([
nested.bind(this, result),
branches
], callback);
} else {
async.waterfall([
making.bind(this, result),
code,
difficult
], callback);
}
},
to.bind(this, 'const1', 'const2')
], function(err, result) {
if(err) return console.log(err);
follow(result);
});
("dealing with nested branches making code difficult to follow")
What if you could write it like this?
q([arg])
.node(dealing, 'error')
.branch(function With(result) {
return result.foo == 'branch1' ? 'branch1' : 'branch2';
});
q('branch1')
.node(nested, 'error')
.node(branches, 'error')
.branch('branchesRejoined');
q('branch2')
.node(making, 'error')
.node(code, 'error')
.node(easy, 'error')
.branch('branchesRejoined');
q('branchesRejoined')
.node(to.bind(this, 'const1', 'const2'), 'error');
.each(follow);
q('error')
.each(console.log);
Tired of this?
someRemoteCall('arg', function recursing(err, result) {
if(err) return console.log(err);
if(result == 'good') {
doSomethingElse();
} else {
someRemoteCall(result, recursing);
// Arg! Stack Overflow!?
// But I can't unroll this recursion because it's an async function, right?
}
});
How about this?
q('recursiveCall')
.node(someRemoteCall, 'error')
.branch(function(result) {
return result == 'good' ? 'doSomethingElse' : 'recursiveCall';
}); // Cannot blow the stack!
q('error')
.each(console.log);
q('doSomethingElse')
...
queue-flow
makes async and sync data processing code easy to read like Fibers
(it doesn't look like fibers code, but it is easy to follow like it) while not altering the Javascript language itself like async
, giving you the best of both worlds, along with some other advantages:
queue-flow
allows its constructor function to be overridden, and sloppy-queue-flow breaks queue order, turning your queue into essentially a task list of operations to perform on independent data.branch
allow you to turn your queues into a flowchart of actions to perform, allowing you to organize your async tasks in easy-to-understand chunks just as functions organize imperative code into easy-to-reuse chunks, and all imperative constructs (if, while, for
) can be replicated with branch
.Still not convinced? Check out more examples on why queue-flow
is better than async
.
Want to learn more? Check out the tutorial for queue-flow
with neat figures explaining the behavior of queue-flow
and its many method verbs. After that, you can read the annotated source code, yourself, which is updated right when new releases of queue-flow
are published (see the nifty prepublish script). (If you're just looking for an API reference, just read the left-hand column and ignore the source code on the right.)
Copyright (C) 2012 by David Ellis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.