What about this for an aync for loop

var forLoop = function(data, worker, callBack) {
if(Array.isArray(data)) {
var returnData = [];
var loop = function(i, cb) {
if(i === data.length) {
return callBack(null, returnData);
}
process.nextTick(function() {
returnData.push(worker.call(data[i++]))
loop(i);
});
//console.log(i)
}
loop(0);
} else {
callBack(new Error('Not an array.'));
}
};
forLoop([{
name : 'sdsad'
}, {
name : 'sdssdfad'
}, {
name : 'sfdsad'
}, {
name : 'sdasad'
}], function() {
//worker function
return this.name// return {firstName:this.name}
}, function(err, result) {
//work done function
console.log(err, result)
});

Maybe could be used to traverse mongoose docs?

On Friday, April 6, 2012 10:38:01 AM UTC-4, linxha wrote:
>
> I'd like to run some user code, which runs in a loop 
>
> pseudo user  code: 
>
> if ... then ... else ... end; 
>  x = ....; 
> blah; 
> blah; 
> blah; 
>
> Think of it as BASIC (it's interpreted). I've found process.nexttick 
> but I'm having difficulty understanding how to take my loop a of lines 
> of user code, loop through them and execute each line with next tick. 
> Obviously I have events being triggered updating the user variables 
> (devices such as arduinos). Does anyone have any pointers?

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to