Node.js fs.promises.appendFile() Method
The fs.promises.appendFile() method of File System module in Node.js is used to interact with the hard disk of the user’s computer. The appendFile() method is used to append new data into the existing file or if the file does not exist then file is created first and after that given data is appended to it. The fs.promises.appendFile() method returns a resolved or rejected promise and hence avoid the callback nesting or callback hell problems that may occur in fs.appendFile() method.
Syntax:
fs.promises.appendFile( path, data, options )
Parameter: This method accepts three parameter path, data and options. Options is an optional parameter.
- path: It is a String, Buffer or URL that specifies the path to the target file in which given data is to be appended.
- data: It is a String or Buffer that is going to append to the target file.
- options: It is an optional parameter that affects the output in someway accordingly we provide it to the function call or not.
- encoding: It specifies the encoding technique, default value is ‘UTF8’.
- mode: It specifies the file mode. File modes allow us to create, read, write, or modify a file. The default value is ‘0o666’.
- flag: It specifies the flag used while appending to the file. The default value is ‘a’.
Return Value: It returns a resolved or rejected promise. The promise is resolved if data is successfully appended to the target file otherwise rejected with an error object if any error is occurred (example-specified file does not have write permission, etc.)
Example 1:
const fs = require( 'fs' )
fs.promises.readFile( './test.txt' )
.then(buff => {
const oldContent = buff.toString()
console.log(`Before Append: ${oldContent}\n`)
return fs.promises.appendFile( './test.txt' ,
'\nHey, I am newly added..!!' )
})
.then(() => {
return fs.promises.readFile( './test.txt' )
})
.then(buff => {
const newContent = buff.toString()
console.log(`After Append: ${newContent}\n`)
})
. catch (err => {
console.log(err)
})
|
we can implement the same functionality using async-await keywords.
const fs = require( 'fs' )
const appendDataToFile = async (path, data) => {
const oldBuffer = await fs.promises.readFile(path)
const oldContent = oldBuffer.toString()
await fs.promises.appendFile(path, data)
const newBuffer = await fs.promises.readFile(path)
const newContent = newBuffer.toString()
console.log(`Before Append: ${oldContent}\n`)
console.log(`After Append: ${newContent}`)
}
appendDataToFile( './test.txt' ,
'\nHey, I am newly added..!!' )
. catch (err => {
console.log(err)
})
|
Output:
Example 2: When a given path to the filename does not exist.
const fs = require( 'fs' )
fs.promises.appendFile( './test.txt' ,
'Please add me to the test file..!!' )
.then(() => {
return fs.promises.readFile( './test.txt' )
})
.then(buff => {
const content = buff.toString()
console.log(`Content : ${content}`)
})
. catch (err => {
console.log(err)
})
|
Implementing the same functionality with async-await keywords.
const fs = require( 'fs' )
const appendDataToFile = async (path, data) => {
await fs.promises.appendFile(path, data)
const buff = await fs.promises.readFile(path)
const content = buff.toString()
console.log(`Content : ${content}`)
}
appendDataToFile( './test.txt' ,
'Please add me to the test file..!!' )
. catch (err => {
console.log(err)
})
|
Directory structure before running the program:
Directory structure after running the program:
Output:
Last Updated :
08 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...