Sometimes, we want to call Node.js s.writeFile
as a promise.
In this article, we’ll look at how to call Node.js fs.writeFile
as a promise.
How to call Node.js fs.writeFile as a promise?
To call Node.js fs.writeFile
as a promise, we can use the fs
‘s promises
module.
For instance, we write
const fs = require('fs').promises;
(async () => {
const file = await fs.readFile('filename.txt', 'utf8');
await fs.writeFile('filename.txt', 'test');
})()
to require the promises
version of fs
with
const fs = require('fs').promises;
Then we call fs.readFile
with path of the file to read and the string with the encoding of the file.
And then we call fs.writeFile
to write to filename.txt
file and 'test'
as its content.
Conclusion
To call Node.js fs.writeFile
as a promise, we can use the fs
‘s promises
module.