Node Callback Concept (original) (raw)

Last Updated : 25 Apr, 2026

In Node.js, callbacks enable asynchronous, non-blocking execution, while methods like readFileSync() perform blocking operations. The fs module provides both synchronous and asynchronous approaches to handle file system tasks.

**Example: Code for reading a file synchronously (blocking code) in Nodejs. Create a text file inputfile1.txt with the following content:

Hello Programmer!!!
Learn NodeJS with GeeksforGeeks

JavaScript `

// Write JavaScript code const fs = require("fs"); const filedata = fs.readFileSync('inputfile1.txt'); console.log(filedata.toString()); console.log("End of Program execution");

`

**Output:

**Example : Code for reading a file asynchronously (non-blocking code) in Nodejs. Create a text file inputfile1.txt with the following content.

Hello Programmer!!!
Learn NodeJS with GeeksforGeeks

JavaScript `

// Write a JavaScript code const fs = require("fs");

fs.readFile('inputfile1.txt', function (ferr, filedata) { if (ferr) return console.error(ferr); console.log(filedata.toString()); } ); console.log("End of Program execution");

`

**Output: