waitfor.it
v0.7.6
Published
Sequential programming for node.js, end of callback hell
Downloads
11
Maintainers
Readme
let wait = require('waitfor.it');
wait.for's little brother with
laxFiber
support
Sequential programming for node.js, end of callback hell.
Simple, straightforward abstraction over Fibers.
By using waitfor.it, you can call any nodejs standard async function in sequential/Sync mode, waiting for result data, without blocking node's event loop (thanks to fibers)
A nodejs standard async function is a function in which the last parameter is a callback: function(err,data)
Advantages:
- Avoid callback hell / pyramid of doom
- Simpler, sequential programming when required, without blocking node's event loop (thanks to fibers)
- Simpler, try-catch exception programming. (default callback handler is: if (err) throw err; else return data)
- No multi-threaded debugging nightmares, only one fiber running at a given time (thanks to fibers)
- Can use any node-standard async function with callback(err,data) as last parameter.
- Plays along with node programming style. Write your async functions with callback(err,data), but use them in sequential/SYNC mode when required.
- Plays along with node cluster. You design for one thread/processor, then scale with cluster on multicores.
Install:
npm i --save waitfor.it
Proper Use:
You need to be in a Fiber to be able to use waitfor.it. The ideal place to launch a fiber is when a request arrives, to handle it:
var server = http.createServer(
function(req, res){
console.log('req!');
wait.createFiber(handler,req,res); //handle in a fiber, keep node spinning
}).listen(8000);
then,at function handler(req,res) and every function you call from there,
you'll be able to use wait.for(ayncFn, arguments...);
Minimal running example
var wait = require('waitfor.it');
function anyStandardAsync(param, callback){
setTimeout( function(){
callback(null,'hi '+param);
}, 5000);
};
function testFunction(){
console.log('fiber start');
var result = wait.for(anyStandardAsync,'test');
console.log('function returned:', result);
console.log('fiber end');
};
console.log('app start');
wait.launchFiber(testFunction);
console.log('after launch');
Generic Usage:
var wait=require('waitfor.it');
// launch a new fiber
wait.createFiber(my_sequential_function, arg, arg, ...);
// in a fiber.. We can wait for async functions
function my_sequential_function(arg,arg...){
// call async_function(arg1), wait for result, return data
var myObj = wait.for(async_function, arg1);
// call myObj.querydata(arg1,arg2), wait for result, return data
var myObjData = wait.forMethod(myObj,'queryData', arg1, arg2);
console.log(myObjData.toString());
}
##Notes on non-standard callbacks. e.g.: connection.query from mysql, database.prepare on node-sqlite3
waitfor.it expects standardized callbacks. A standardized callback always returns (err,data) in that order.
A solution for the sql.query method and other non-standard callbacks is to create a wrapper function standardizing the callback, e.g.:
connection.prototype.q = function(sql, params, stdCallback){
this.query(sql,params, function(err,rows,columns){
return stdCallback(err,{rows:rows,columns:columns});
});
}
usage:
try {
var result = wait.forMethod(connection, "q", options.sql, options.params);
console.log(result.rows);
console.log(result.columns);
}
catch(err) {
console.log(err);
}
##How does wait.launchFiber works?
wait.launchFiber(genFn,param1,param2)
starts executing the function genFn
as a fiber-generator until a "yield" (wait.for) is found, then wait.launchFiber
executes the "yielded" value (a call to an async function), and links generator's "next" with the async callback(err,data), so when the async finishes and the callback is called, the fiber/generator "continues" after the var x =wait.for(...)
.