Given this code:
var something = function(callback) {
if(condition) {
Mongoose.findOne(id, function(err, doc) {
if(doc) {
callback(doc);
} else {
callback();
}
});
} else {
callback();
}
}
How would I rewrite it in a cleaner way so that 'callback' is just called in one place. I assume I can wrap this entire thing somehow and do that - I've seen it but cannot get it quite right.
if (doc) {...and just docallback(doc)? Does your callback look atarguments.length? If so, then you could docallback.apply(null, [].slice.call(arguments, 1))if you really want.callCallbackwhere you put all the code related to calling thecallbackand then call that method from all the three places with necessary parameters