I’m trying to get my head around when to use process.nextTick. Below I’m using async library to control my code flow and was wondering if I should be calling nextTick in the end callback or not.
async.parallel([
function (callback) {
// do something
callback(data);
},
function (callback) {
// do something
callback(data);
}
],
// callback
function (data) {
process.nextTick(function () {
// do something
});
});
Without knowing exactly what your ‘do something’ is, I don’t see any reason that you’d need to use
nextTickin this situation. You usenextTickwhen you have reason to defer execution of a function to the next iteration of the event loop. You might want to read Understandingprocess.nextTickfor more detail.Also, note that async’s
paralleltask completion callbacks take argumentserr, data, so you should be doing:The major difference being that the final callback is invoked immediately if one of the parallel tasks returns an error (calls
callbackwith a truthy first argument).