The fs.appendFile() 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 appendFile() method is used to append new data in the existing file or if the file does not exist then the file is created first and after that given data is appended to it.
The fs.appendFile() 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 like to work with a promise-based method. Using some extra node.js method, we can operate a callback-based method in promise way.
fs.appendFile(path, data, options)
- 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 which affects the output in someway accordingly we provide it to the function call or not.
- encoding: It specifies the encoding technique, default 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’.
Approach: The fs.appendFile() method based on callback. To operate it with promises, first, we use promisify() method defined in the utilities module to convert it into a promise based method.
Implementing the same functionality using async-await.
Example 2: When a given path to the filename does not exist.
Implementing the same functionality as async-await.
Directory structure before running the program:
Directory structure after running the program:
- How to operate callback-based fs.opendir() method with promises in Node.js ?
- How to operate callback-based fs.mkdir() method with promises in Node.js ?
- How to operate callback based fs.writeFile() method with promises in Node.js ?
- How to operate callback-based fs.readdir() method with promises in Node.js ?
- How to operate callback-based fs.lstat() method with promises in Node.js ?
- How to operate callback-based fs.rename() method with promises in Node.js ?
- How to operate callback-based fs.truncate() method with promises in Node.js ?
- How to operate callback-based fs.readFile() method with promises in Node.js ?
- Node.js fs.promises.link() Method
- Node.js | fs.promises.appendFile() Method
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs console.timeLog() Method
- ES6 | Promises
- Promises in Node.js
- How to use Typescript with native ES6 Promises ?
- Implementing callback in PHP
- What is callback hell in Node.js ?
- jQuery | Callback Functions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.