The Wayback Machine - https://web.archive.org/web/20240821133315/https://www.geeksforgeeks.org/node-js-util-promisify-method/
Open In App

Node util.promisify() Method

Last Updated : 14 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

`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
Node.js util.types.isSymbolObject() Method
The util.types.isSymbolObject() 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.isSymbolObject() method is used to determine whether the value is a symbol object. Syntax: util.types.isSymbolObject( value ) Parameter: This method a
2 min read
Node.js util.types.isNumberObject() Method
The util.types.isNumberObject() method of util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed value is a Number object or not. Syntax util.types.isNumberObject( value ) Parameters: This method accepts a single parameter value which holds any number i.e. instance of any module.
2 min read
Node.js util.types.isRegExp() Method
The util.types.isRegExp() method of util module which is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed value is a regular expression. Syntax: util.types.isRegExp( value ) Parameters: This method accepts a single parameter value which holds any value i.e instance of any module. Return va
2 min read
Node.js util.types.isInt32Array() Method
The util.types.isInt32Array() method of the util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in Int32Array instance or not. Syntax: util.types.isInt32Array( value ) Parameters: This method accepts a single parameter value that holds any valu
2 min read
Node.js util.types.isUint8Array() Method
The util.types.isUint8Array() method of the util module which is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in Uint8Array instance or not. Syntax: util.types.isUint8Array( value ) Parameters: This method accepts a single parameter value that holds an
2 min read
Node.js util.types.isStringObject() Method
The util.types.isStringObject() method of the util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a String object or not. There is a difference between a string primitive and a String object in javascript, string is a primitive datatype it has no metho
2 min read
Node.js util.types.isSet() Method
The util.types.isSet() method of the util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in Set instance or not. Syntax: util.types.isSet( value ) Parameters: This method accepts a single parameter value that holds any value i.e instance of any
2 min read
Node.js | util.types.isSharedArrayBuffer() Method
The util.types.isSharedArrayBuffer() method of util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in SharedArrayBuffer instance or not. Syntax: util.types.isSharedArrayBuffer( value ) Parameters: This method accepts a single parameter value wh
2 min read
Node.js util.types.isGeneratorFunction() Method
The util.types.isGeneratorFunction() 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.isGeneratorFunction() method is used to check if the given value is a generator function or not.Syntax: util.types.isGeneratorFunction( value )Paramet
2 min read
Node.js | util.types.isArgumentsObject() Method
The util.types.isArgumentsObject() 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.isArgumentsObject() method is used to check if the given value is an arguments object or not.Syntax: util.types.isArgumentsObject( value ) Parameters: T
2 min read
Node.js | util.types.isDataView() Method
The util.types.isDataView() method method of util module is primarily designed to support the needs of Node.js own Internal APIs. The util.types.isDataView() method is used to check if the given value is a DataView object or not. Syntax: util.types.isDataView(value) Parameters: This function accepts one parameter as mentioned above and described be
2 min read
Node.js util.types.isBigUint64Array() Method
The util.types.isBigUint64Array() method of util module is primarily designed to support the needs of Node.js own Internal APIs. The util.types.isBigUint64Array() method is used to check if the given value is a BigUint64Array object. Syntax: util.types.isBigUint64Array( value ) Parameters: This function accepts one parameter as mentioned above and
2 min read
Node.js util.types.isBoxedPrimitive() Method
The util.types.isBoxedPrimitive() 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.isBoxedPrimitive() method is used to check if the given value is a boxed primitive or not. Syntax: util.types.isBoxedPrimitive( value ) Parameters: This
2 min read
Node.js util.types.isGeneratorObject() Method
The util.types.isGeneratorObject() 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.isGeneratorObject() method is used to check if the given value is a generator object or not. Syntax: util.types.isGeneratorObject( value ) Parameters:
2 min read
Node.js util.types.isUint8ClampedArray() Method
The util.types.isUint8ClampedArray() 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.isUint8ClampedArray() method is used to check if the given value is an unsigned 8-bit clamped integer array or not. Syntax: util.types.isUint8Clamped
2 min read
Node.js util.types.isProxy() Method
The util.types.isProxy() 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.isProxy() method is used to check if the given value is a proxy instance or not. Syntax: util.types.isProxy( value ) Parameters: This function accepts single par
2 min read
Node.js util.types.isAsyncFunction() Method
The util.types.isAsyncFunction() method is an inbuilt application programming interface of the util module which is used to type check for asynchronous functions in the node.js. Syntax: util.types.isAsyncFunction( value ) Parameters: This method accepts a single parameter as mentioned above and described below: value: It is a required parameter tha
2 min read
Node.js | util.types.isBigInt64Array() Method
The util.types.isBigInt64Array() method is an inbuilt application programming interface of the util module which is used to type check for BigInt64Array in the node.js. Syntax: util.types.isBigInt64Array( value ) Parameters: This method accepts single parameter as mentioned above and described below: value: It is a required parameter and of any dat
2 min read
Node.js util.types.isArrayBuffer() Method
The util.types.isArrayBuffer() method is an inbuilt application programming interface of the util module which is used to check for built-in ArrayBuffer type objects in the node.js. Syntax: util.types.isArrayBuffer( value ) Parameters: This method accepts single parameter as mentioned above and described below: value: It is a required parameter of
1 min read
Node.js util.types.isAnyArrayBuffer() Method
The util.types.isAnyArrayBuffer() method is an inbuilt application programming interface of the util module which is used to perform type checking for any built-in ArrayBuffer objects in the node.js. Syntax: util.types.isAnyArrayBuffer( value ) Parameters: This method accepts single parameter as mentioned above and described below: value: It is a r
2 min read
three90RightbarBannerImg