How to operate callback-based fs.mkdir() method with promises in Node.js ?
The fs.mkdir() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user’s computer. The mkdir() method is used to asynchronously create a directory.
The fs.mkdir() method is based on callback. Using callback methods leads to a great chance of callback nesting or callback hell problems. Thus to avoid it we almost always like to work with a promise-based method. Using some extra node.js methods we can operate a callback-based method in promise way.
Syntax:
fs.mkdir(path, options)
Note: Callback not required since we operate the method with promises.
Parameters: This method accept two parameters as mentioned above and described below:
- path: It is a String, Buffer or URL that specifies the path to the directory that has to be created.
- options: It is an optional parameter which affects the output in someway accordingly we provide it to the function call or not.
- mode: It is a string or an integer which used to set the directory permission. By default its value is 0.777
- recursive: It is a boolean value which specifies whether parent directories should be created or not. By default its value is false.
Approach: The fs.mkdir() method based on callback. To operate it with promises, first, we use promisify() method defined in utilities module to convert it into a promise based method.
Example 1: Filename: index.js
const fs = require( 'fs' )
const util = require( 'util' )
const makeDir = util.promisify(fs.mkdir)
const readDir = util.promisify(fs.readdir)
makeDir(dir= './Test Directory' )
.then(() => {
console.log(`Directory '${dir}' is created`)
})
. catch (err => {
console.log(`Error occurs,
Error code -> ${err.code},
Error No -> ${err.errno}`);
})
|
Implementing the same functionality using async await:
const fs = require( 'fs' )
const util = require( 'util' )
const makeDir = util.promisify(fs.mkdir)
const readDir = util.promisify(fs.readdir)
const createDirectory = async path => {
await makeDir(path)
console.log(`Directory '${path}' is created`)
}
createDirectory( './TestDirectory' )
. catch (err => {
console.log(`Error occurs,
Error code -> ${err.code},
Error No -> ${err.errno}`);
})
|
Run the index.js file using the following command:
node index.js
File contents before running the program :
File contents after running the program:
Output:
Directory './Test Directory' is created
Example 2: Filename: index.js
const fs = require( 'fs' )
const util = require( 'util' )
const makeDir = util.promisify(fs.mkdir)
const readDir = util.promisify(fs.readdir)
console.log(`\nBefore creating new Directory : \n`)
readDir(process.cwd())
.then(filenames => {
for (let filename of filenames){
console.log(filename)
}
})
. catch (err => {
console.log(`Error occurs,
Error code -> ${err.code},
Error No -> ${err.errno}`);
})
makeDir( './Test Directory' )
.then(() => {
console.log(`\nAfter creating new directory : \n`)
return readDir(process.cwd())
})
.then(filenames => {
for (let filename of filenames) {
console.log(filename)
}
})
. catch (err => {
console.log(`Error occurs,
Error code -> ${err.code},
Error No -> ${err.errno}`)
})
|
Implementing same functionality using async await:
const fs = require( 'fs' )
const util = require( 'util' )
const makeDir = util.promisify(fs.mkdir)
const readDir = util.promisify(fs.readdir)
const createDirectory = async path => {
console.log(`\nBefore creating new Directory : \n`)
const oldContents = await readDir(process.cwd())
for (let filename of oldContents) {
console.log(filename)
}
await makeDir( './Test Directory' )
console.log(`\nAfter creating new directory : \n`)
const newContents = await readDir(process.cwd())
for (let filename of newContents) {
console.log(filename)
}
}
createDirectory( './TestDirectory' )
. catch (err => {
console.log(`Error occurs,
Error code -> ${err.code},
Error No -> ${err.errno}`);
});
|
Run the index.js file using the following command:
node index.js
File contents before running the program:
File contents after running the program:
Output:
Last Updated :
18 Jul, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...