Skip to content
Related Articles

Related Articles

Node.js | util.promisify() Method
  • Last Updated : 11 Jun, 2020

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 module
const util = require('util')
  
// Importing File System module
const fs = require('fs')
  
// Use promisify to convert callback
// based method fs.readdir to 
// promise based method
const 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 module
const util = require('util')
   
// Importing File System module
const fs = require('fs')
   
// Use promisify to convert callback
// based method fs.readdir to 
// promise based method
const 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 module
const util = require('util')
  
importing File System module
const fs = require('fs')
  
// Use promisify to convert 
// callback based methods to 
// promise based methods
const 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

My Personal Notes arrow_drop_up
Recommended Articles
Page :