Skip to content

err first callback vs rejects in promises

Course: Promises and async, await - chapter 9

fs.readFile function used an err first callback approach to propagate back errors

const fs = require("fs")
function afterDone(err, data) {
if (err) {
console.log("Error while reading file");
} else {
console.log(data)
}
}
fs.readFile("a.txt", "utf-8", afterDone);

Promises use the reject argument to propagate errors

const fs = require("fs");
function readFilePromisified(filePath) {
return new Promise(function (resolve, reject) {
fs.readFile(filePath, "utf-8", function (err, data) {
if (err) {
reject("Error while reading file");
} else {
resolve(data);
}
});
});
}
function onDone(data) {
console.log(data);
}
function onError(err) {
console.log("Error: " + err);
}
readFilePromisified("a.txt").then(onDone).catch(onError);