Node util.promisify() Method Last Updated : 07 Jan, 2025 Comments Improve Suggest changes Like Article Like 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 </p><pre><code class="language-javascript"></code></pre><p></p><pre></pre><p><br></p><pre><code><span>// Node.js program to illustrate </span></code><br><code><span>// util.promisify() methods </span></code><br><br><code><span>// Since promisify function </span></code><br><code><span>// returns promise version </span></code><br><code><span>// of a function, it can also </span></code><br><code><span>// operate using async and await </span></code><br><br><code><span>// Importing Utilities module </span></code><br><code><span>const util = require('util')</span></code><br><br><code><span>// Importing File System module </span></code><br><code><span>const fs = require('fs')</span></code><br><br><code><span>// Use promisify to convert callback </span></code><br><code><span>// based method fs.readdir to </span></code><br><code><span>// promise based method </span></code><br><code><span>const readdir = util.promisify(fs.readdir)</span></code><br><br><code><span>const readFiles = async (path) => {</span></code><br><code><span> const files = await readdir(path)</span></code><br><code><span> console.log(files)</span></code><br><code><span>}</span></code><br><br><code><span>readFiles(process.cwd()).catch(err => {</span></code><br><code><span> console.log(err)</span></code><br><code><span>}) </span></code></pre><p dir="ltr"><br></p><p dir="ltr"><b><strong>Output:</strong></b></p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20200604121930/output-of-programme-to-illsutrate-promisify.png" width="1220" height="493"><p dir="ltr"><b><strong>Example 3: </strong></b><span>Below is the code example of the util.promisify() Method:</span></p><gfg-tabs data-run-ide="false" data-mode="light"><gfg-tab slot="tab">JavaScript</gfg-tab><gfg-panel slot="panel" data-code-lang="javascript"><pre><code class="language-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. Comment More infoAdvertise with us Next Article Node.js util.isDeepStrictEqual() Method H hunter__js Follow Improve Article Tags : JavaScript Web Technologies Node.js C++-Virtual Functions Java-NavigableMap TCS-coding-questions Node.js-util-module +3 More Similar Reads Node.js Utility Module The util module in Node.js provides a variety of utility functions that assist with tasks such as debugging, formatting, and inheritance. It includes methods for inspecting objects, formatting strings, and extending classes. Node.js Utility ModuleThe util module offers essential utilities that are n 4 min read Node.js util.callbackify() Method The util.callbackify() method is an inbuilt application programming interface of the util module which is used to run an asynchronous function and get a callback in the node.js.Syntax:Â Â util.callbackify( async_function ) Parameters: This method accepts single parameter as mentioned above and descri 2 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 f 3 min read Node.js util.format() Method The util.format() (Added in v0.5.3) method is an inbuilt application programming interface of the util module which is like printf format string and returns a formatted string using the first argument. The formatted string contains zero or more format specifiers in which the corresponding argument v 5 min read Node.js util.inherits() 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.inherits() (Added in v0.3.0) method is an inbuilt application programming interface of the util module in which the constructor inh 3 min read Node.js util.formatWithOptions() Method The util.formatWithOptions() (Added in v10.0.0) method is an inbuilt application programming interface of the util module which is like printf format string and returns a formatted string using the first argument. It is somewhat identical to util.format() method, the exception in this method is that 4 min read Node.js util.inspect() 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.inspect() (Added in v0.3.0) method is an inbuilt application programming interface of the util module which is intended for debugging 7 min read Node util.promisify() Method `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)Paramete 2 min read Node.js util.isDeepStrictEqual() 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.isDeepStrictEqual() (Added in v9.0.0) method is an inbuilt application programming interface of the util module which is an exporte 3 min read Node.js util.deprecate() Method The util.deprecate() (Added in v0.8.0_ method is an inbuilt application programming interface of the util module which wraps fn (which may be a function or class) in such a way that it is marked as deprecated. When util.deprecate() method is called, it returns a function that emits a DeprecationWarn 3 min read Like