Async.js contains a bunch of helper functions like foreach and whilst that do exactly what you ask for here.
Edit:
Here is a complete example:
async = require('async');
var i = 0
async.whilst(
function() { return i < 5; },
function(callback) {
i++;
console.log('doing stuff.');
process.nextTick(callback);
},
function (err) {
console.log('done!');
}
);
What outputs:
doing stuff.
doing stuff.
doing stuff.
doing stuff.
done!
Edit 2: Named functions for comments have been changed to not confuse people.