Created
August 11, 2024 11:00
-
-
Save ujshaikh/d587791b351d0069e103a59063fb995c to your computer and use it in GitHub Desktop.
An example of callback function with the help of fs method
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 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