Is there a concrete reason to prefer executing callbacks to functions before or after exiting the called function? Example
function doSomethingPossiblyAsync(callback) {
if (errorDetectedImmediately) {
// call callback immediately before doSomethingPossilbyAsync has returned
callback(someError);
}
...
}
vs
function doSomethingPossiblyAsync(callback) {
if (errorDetectedImmediately) {
// call callback after doSomethingPossiblyAsync has returned
process.nextTick(function() {
callback(someError);
}
}
...
}
I prefer the second because it makes it consistent with the asynciness of the non-error path but I have no idea if I'm being rational or if it doesn't matter.
Note that for EventEmttiers it matters since you can't add your listeners before you've created the object which means you shouldn't emit events until after creation even if detected during creation but does some similarly logical reason apply to callbacks?