We have used the after
module to handle the parallel task, and this module does internal checks to ensure that the callback
function has not already been executed. This check usually needs to be done because a function could be called with an error argument, triggering the callback function to be executed early. If the callback function has been run with an error parameter, we don't care about sending the final result after all the tasks have finished, because we expect the callback function to be executed only once.
Let's take a moment to reflect on the done
callback function before using the after
module to modify it:
var doneCalled = false; var tasksCount = 2; var done = function(err) { if (doneCalled) { return; } if (err) { doneCalled = true; return callback(err); }; tasksCount--; if (tasksCount === 0) { movieInfo.trailers = trailers; movieInfo.cast = cast; callback(null, movieInfo); } };
There is a doneCalled
Boolean...