I tried a lot to use aync library in nodejs but my simpliest test even don't works.
var async = require('async');
var i = 0 ;
var inc = function(){
i++;
} ;
var show = function(){
console.log(i) ;
} ;
var err = function(err){
console.log(err) ;
} ;
async.forever(inc,err) ;
async.forever(show,err) ;
Function don't loop at all and the output is only
1
What i'm doint wrong please.
async.forever() passes a callback to show and inc that they need to call:
var inc = function(done){
i++;
setImmediate(done);
};
var show = function (done) {
console.log(i);
setImmediate(done);
};
Using setImmediate (or setTimeout) makes the call asynchronous so you don't get a stack overflow error:
var inc = function(done){
i++;
done();
};
RangeError: Maximum call stack size exceeded
var inc = function(done){
^
And, keep in mind that the async library doesn't make tasks asynchronous; it assumes the tasks are already asynchronous and simply helps manage them. Using synchronous tasks with async will still block in most cases.