Open In App

Node util.promisify() Method

Last Updated : 14 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

`util.promisify()` in Node.js converts callback-based methods to promise-based, aiding in managing asynchronous code more cleanly. This alleviates callback nesting issues, enhancing code readability, and simplifying asynchronous operations through promise chaining.

Syntax:

util.promisify(func)

Parameters: This method accepts a single parameter function that holds the callback based function.

Return Value: This method returns a promise-based function.

Example 1: Below is the code example of the util.promisify() Method:

Javascript




// 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: Below is the code example of the util.promisify() Method:

Javascript




// 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: Below is the code example of the util.promisify() Method:

Javascript




// 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:

We have a Cheat Sheet on Node utility methods where we covered all the utility methods to check those please go through Node.js Utility Complete Reference article.



Previous Article
Next Article

Similar Reads

How to Promisify geolocation API to get current position using JavaScript ?
In this article, we are going to use Promisify geolocation API into Promise-based API. Prerequisite: JavaScript Promise Approach: As we know the navigator.geolocation.getCurrentPosition is a callback-based API, so we can easily convert it into a Promise-based API. To promisify the geolocation API, we will get the user's current position from the br
3 min read
Node.js util.debuglog() Method
The “util” module provides ‘utility’ functions that are used for debugging purposes. For accessing those functions we need to call them (by ‘require(‘util’)‘). The util.debuglog() (Added in v0.11.3) method is an inbuilt application programming interface of the util module which is used to create a function that is based on the NODE_DEBUG environmen
3 min read
Node.js util.types.isMap() Method
The util.types.isMap() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isMap() method is used to determine whether the value is a built-in Map instance or not. Syntax: util.types.isMap( value ) Parameters: This method accepts a single
2 min read
Node.js util.types.isMapIterator() Method
The util.types.isMapIterator() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js internal APIs. The util.types.isMapIterator() method is used to determine whether the value is an iterator returned for a built-in Map instance or not. Syntax: util.types.isMapIterator( val
2 min read
Node.js util.types.isSetIterator() Method
The util.types.isSetIterator() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isSetIterator() method is used to determine whether the value is an iterator returned for a built-in set instance. Syntax: util.types.isSetIterator( value
2 min read
Node.js util.types.isWeakMap() Method
The util.types.isWeakMap() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isWeakMap() method is used to determine whether the value is a built-in WeakMap instance or not. Syntax: util.types.isWeakMap( value ) Parameters: This method
2 min read
Node.js util.types.isWeakSet() Method
The util.types.isWeakSet() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isWeakSet() method is used to determine whether the value is a built-in WeakSet instance or not. Syntax: util.types.isWeakSet( value ) Parameters: This method
2 min read
Node.js util.types.isBooleanObject() Method
The util.types.isBooleanObject() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isBooleanObject() method is used to determine whether the value is a Boolean object or not. Syntax: util.types.isBooleanObject( value ) Parameters: This
2 min read
Node.js util.types.isPromise() Method
The util.types.isPromise() method is an inbuilt application programming interface of the util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isPromise() method is used to determine whether the value is a built-in Promise. Syntax: util.types.isPromise( value ) Parameter: This method accepts a sin
2 min read
Node.js util.types.isTypedArray() Method
The util.types.isTypedArray() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isTypedArray() method is used to determine whether the value is a built-in TypedArray instance. Syntax: util.types.isTypedArray( value ) Parameter: This met
1 min read