Node.js | util.promisify() Method

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  })

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  })

chevron_right


Output:

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
})

chevron_right


Output:

Reference: https://nodejs.org/api/util.html#util_util_promisify_original




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.