Published at
Updated at
Reading time
1min

This post is part of my Today I learned series in which I share all my learnings regarding web development.

That's a really quick one. 🙈

When writing Node.js scripts and using the fs module I usually used the util.promisify method to promisify the methods that I needed.

Today I learned that fs since Node.js 11 provides the methods in a promises property. 🎉

// old way have using promise-based fs methods
const { readFile } = require("fs");
const { promisify } = require('util');
const promisifiedReadFile = promisify(readFile);

promisifiedReadFile(__filename, { encoding: "utf8" })
  .then(data => console.log(data));

// --------------------

// new way of using promise-based fs methods
// no util.promisify!!!
const { readFile } = require("fs").promises;
readFile(__filename, { encoding: "utf8" })
  .then(data => console.log(data));

Using the promises property you can now skip one step and there is no need to use promisify. That's excellent news!

Related Topics

Related Articles