In the previous article, we checked out some of the util module functionalities including the inspect(), the format(), and the debuglog methods. But there are more to reveal in this article, and we are going to touch on some of the new features that Node.js v8 is introducing in that module.

The inherits Method

util.inherits() inherits the prototype from a parent constructor into a child constructor. So basically the child constructor will have the methods and properties of the parent constructor.

It gets to be applied like so util.inherits(childConstructor, parentConstructor). Besides that, the parentConstructor is accessible through the childConstructor.super_ property. Let's take an example.

const util = require('util');

function Employee() {
    //...
}

Employee.prototype.work = function () {
    console.log('Working!');
};

function Manager() {
    //...
}

util.inherits(Manager, Employee);

manager = new Manager();

manager.work(); // Working!

console.log(manager instanceof Employee); // true
console.log(Manager.super_ === Employee); // true

The Manager constructor now has inherited the prototype of the Employee constructor, and that has been shown in the last two statements where true has been printed for each statement. Apparently, all the Employee methods are available for the Manager. Therefore, calling the work() method executed the one that was attached to the Employee prototype.

The Node.js team mentioned that the usage of util.inherits() is discouraged, and that ES6 class and extends keywords should be utilized instead to get language level inheritance support.

Let's employ ES6 classes to achieve the inheritance the is recommended.

class Employee {
    work() {
        console.log('Working!');
    }
}

class Manager extends Employee {
    // ...
}

manager = new Manager();

manager.work(); // Working!

console.log(manager instanceof Employee); // true

The callbackify Method

The util.callbackify() method has been introduced in Node.js v8.2.0 and it kind of cool approach. It takes an async function (async function or a function that returns a Promise) returning a function as the old Node.js callback function style. In the callback, the first argument will be the rejection reason (or null if the Promise resolved), and the second argument will be the determined value. Let's see that in an example.

const util = require('util');

async function fn() {
    return await Promise.resolve('hello world');
}

const callbackFunction = util.callbackify(fn);

callbackFunction((err, ret) => {
    if (err) throw err;
    console.log(ret);
});

The async function fn returing a "resolved" Promise, we pass that function to the callbackify() which will return a callback function.

The promisify Method

The util.promisify() method, which was introduced in Node.js v8.0.0, is a complementary and correlative for callbackify which takes a function following the standard Node.js callback style and returns a version that returns promises. util.promisify(original) assumes that original is a function taking a callback as its final argument in all cases, and the returned function will result in undefined behavior if it does not.

const util = require('util');
const fs = require('fs');

const stat = util.promisify(fs.stat);
stat('.').then((stats) => {
    // Do something with `stats`
}).catch((error) => {
    // Handle the error.
});

Or using the async function.

const util = require('util');
const fs = require('fs');

const stat = util.promisify(fs.stat);

async function callStat() {
    const stats = await stat('.');
    console.log(`This directory is owned by ${stats.uid}`);
}