Created
July 30, 2021 16:55
-
-
Save heypano/b015e709664550355c5236384ba9f1b0 to your computer and use it in GitHub Desktop.
Read/Write file with promises in node js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { promises } = require("fs"); | |
const { readFile, writeFile } = promises; | |
const input = "src/index.js"; | |
const output = "src/index2.js"; | |
const errorHandler = (error) => { | |
console.error(error.message); | |
process.exit(1); | |
}; | |
readFile(input) | |
.then((fileBuffer) => { | |
console.log(fileBuffer.toString()); | |
writeFile(output, fileBuffer.toString().split("").reverse().join("")) | |
.then((r) => { | |
console.log("Saved file", r); | |
}) | |
.catch(errorHandler); | |
}) | |
.catch(errorHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment