Skip to content

Instantly share code, notes, and snippets.

@ujshaikh
Created August 11, 2024 11:00
Show Gist options
  • Save ujshaikh/d587791b351d0069e103a59063fb995c to your computer and use it in GitHub Desktop.
Save ujshaikh/d587791b351d0069e103a59063fb995c to your computer and use it in GitHub Desktop.
An example of callback function with the help of fs method
const fs = require("fs");
const path = require("path")
// Resolve file path
const filePath = path.resolve("../", "data", "file.txt")
// Read file asynchronously
// 3rd params of this method is a callback function, which will return error and data
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}
console.log("File content:", data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment