Node.js util.promisify() Method

08 Oct, 2021
The util.promisify() method defines in utilities module of Node.js standard library. It is basically used to convert a method that returns responses using a callback function to return responses in a promise object. Usually, very soon it becomes very difficult to work with callbacks due to callback nesting or callback hells. It becomes very difficult to organize or format our code so that other developers if working with that code, can understand it easily. In other side, it is very easy to deal with promises as nesting promises are also operate in linear style i.e. promise chaining. The util.promisify() method does this for us and makes the method to operate with promises.
Syntax:
util.promisify(func)
Parameters: This method accepts a single parameter func that holds the callback based function.
Return Value: This method returns a promise based function.
Example 1:
// Node.js program to illustrate // util.promisify() methods  // Importing Utilities moduleconst util = require('util')  // Importing File System moduleconst fs = require('fs')  // Use promisify to convert callback// based method fs.readdir to // promise based methodconst readdir = util.promisify(fs.readdir)  readdir('process.cwd()')  .then(files => {    console.log(files)  })  .catch(err => {    console.log(err)  })
Output:
[Error: ENOENT: no such file or directory, 
scandir 'C:\Users\bhudk\Desktop\nodec\process.cwd()'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'scandir',
  path: 'C:\\Users\\bhudk\\Desktop\\nodec\\process.cwd()'
}
Example 2:
// Node.js program to illustrate // util.promisify() methods  // Since promisify function // returns promise version // of a function, it can also// operate using async and await   // Importing Utilities moduleconst util = require('util')   // Importing File System moduleconst fs = require('fs')   // Use promisify to convert callback// based method fs.readdir to // promise based methodconst readdir = util.promisify(fs.readdir)   const readFiles = async (path) => {    const files = await readdir(path)    console.log(files)  }     readFiles(process.cwd()).catch(err => {    console.log(err)  })
Output:
Example 3:
// Node.js program to illustrate // util.promisify() methods  // Importing Utilities moduleconst util = require('util')  importing File System moduleconst fs = require('fs')  // Use promisify to convert // callback based methods to // promise based methodsconst readdir = util.promisify(fs.readdir)const lstat = util.promisify(fs.lstat)    const readFiles = async (path) => {  const files = await readdir(path)  for (let file of files) {    const stats = await lstat(file)    if (stats.isFile()) {      console.log(`${file} -----> File`)    } else {      console.log(`${file} -----> Folder`)    }  }}  readFiles('process.cwd()').catch(err => {  console.log(err)})
Output:
Reference: https://nodejs.org/api/util.html#util_util_promisify_original
Node.js-util-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React