In node.js asynchronous functions have callback, however only some of them have err argument passed to that function. e.g. fs.writeFile has err as parameter
fs.writeFile('message.txt', 'Hello Node', function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
but fs.watchfile doesn't
fs.watchFile('message.text', function (curr, prev) {
console.log('the current mtime is: ' + curr.mtime);
console.log('the previous mtime was: ' + prev.mtime);
});
First question is why some async functions have err argument in callback and some don't? How are we supposed to handle error of those which don't?
Also, as far as synchronous functions are concerned, are they all emitting "error" event which we can subscribe to and handle error this way?
var rs = fs.createReadStream("C:\\Temp\\movie.mp4");
rs.on('error', function(err) {
console.log('!error: ', err);
});
and last question: most of synchronous functions have Sync in name... why createReadStream does not?
Thanks!