I got confused with async nature of node.js. I'm writing a small project in it and my previous (sync) programming experience gets in the way.
How should I decide if/where to write async code?
For example I've got a model with fields, where each has some validation rules:
model = { title: text_validation,
subtitle: text_validation }
text_validation = { max_len: 10,
required: true,
url: true }
When I validate the model, I iterate through all fields checking if rules pass - these are really quick functions.
Model.validate = function() {
validator = {};
fields = Object.keys(Model);
fields.forEach(function(field) {
validator[field_name] = field.validate();
});
}
Field.validate = function() {
validator = [];
rules.forEach(function(rule) {
if (rule is not valid)
validator.push(rule)
});
return validator;
}
Should I use callbacks with such short and quick iterations?
Where is the limit here? Should node.js be always async or can I allow sync loops or w/e if it's quick enough? Please, if possible refer to examples when and where to use sync/async.
process.nextTick) and definetly there is no serious advantage of doing that in most cases. Unless your loop is taking really long time.