fs.writeFile will pass an error to your callback function as err in the event an error happens.
Consider this example
function wakeUpSnorlax(done) {
// simulate this operation taking a while
var delay = 2000;
setTimeout(function() {
// 50% chance for unsuccessful wakeup
if (Math.round(Math.random()) === 0) {
// callback with an error
return done(new Error("the snorlax did not wake up!"));
}
// callback without an error
done(null);
}, delay);
}
// reusable callback
function callback(err) {
if (err) {
console.log(err.message);
}
else {
console.log("the snorlax woke up!");
}
}
wakeUpSnorlax(callback);
wakeUpSnorlax(callback);
wakeUpSnorlax(callback);
2 seconds later ...
the snorlax did not wake up!
the snorlax did not wake up!
the snorlax woke up!
In the example above, wakeUpSnorlax is like fs.writeFile in that it takes a callback function to be called when fs.writeFile is done. If fs.writeFile detects and error during any of its execution, it can send an Error to the callback function. If it runs without any problem, it will call the callback without an error.
writeFilemethod. That method receives it, and at the right time, it calls it, passing a value to theerrparameter. If there was no error to report, it'll probably passnullorundefined. I don't remember which.