Skip to content

Instantly share code, notes, and snippets.

@heypano
Created July 30, 2021 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heypano/b015e709664550355c5236384ba9f1b0 to your computer and use it in GitHub Desktop.
Save heypano/b015e709664550355c5236384ba9f1b0 to your computer and use it in GitHub Desktop.
Read/Write file with promises in node js
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