In our previous discussions, Mytour introduced REPL and NPM in Node.js. Continuing the Node.js journey, this article by Mytour delves deeper into the concept of Callbacks in Node.js.
Exploring the Concept of Callbacks in Node.js
What is Callback in Node.js?
Callback in Node.js is a type of asynchronous function. It is invoked after completing a specific task. Node utilizes callbacks extensively, with all Node APIs written in the callback style.
To better understand callback functions in Node.js, imagine a function to read a file that initiates the file reading process and returns control to the environment for the next command execution. Upon completion of file I/O, it invokes the callback function, utilizing the file content as a parameter. Thus, there's no blocking or waiting when reading/writing files.
Essentially, callback functions enable Node.js to execute a higher volume of requests without waiting for the results to return.
Illustration of Blocking Code
Create a text file named input.txt containing the following content:
Tutorials Point provides educational material for self-study
to educate the global community in a simple and accessible manner!!!!!
Create a file named js and name it main.js
var fs = require('fs');
var data = fs.readFileSync('input.txt');
console.log(data.toString());
console.log('Program Ended');
Next, execute the file main.js to view the result by opening the Node.js command prompt and entering the following command:
$ node main.js
Verify the output:
Tutorials Point provides educational material for self-study
to educate the global community in a simple and accessible manner!!!!!
Program Ended
Illustration of Non-Blocking Code
Create a text file named input.txt containing the following content:
Tutorials Point provides educational material for self-study
to educate the global community in a simple and accessible manner!!!!!
Update the file main.js using the following code:
var fs = require('fs');
fs.readFile('input.txt', function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});
console.log('Program Ended');
Next, open the Node.js command prompt and enter the following command to run main.js and view the result:
$ node main.js
The two examples above illustrate the mechanisms of blocking and non-blocking calls. In detail:
- The first example demonstrates the program will block until it reads the file and then terminate.
- The second example shows the program doesn't wait for the file reading and prints 'Program Ended' simultaneously; the program continues without blocking to read the file.
In summary, blocking programs execute multiple steps and different sequences, suitable for executing logical commands. Conversely, non-blocking programs do not execute in any specific order. In cases where the program needs to use any data for processing, that data will be stored in the blocking program and executed in sequence.
The article above from Mytour just introduced you to Callback in Node.js. In the next articles, Mytour will further introduce you to Events in Node.js.
Additionally, if there are any inquiries or questions needing clarification, readers can leave their comments below the article for further discussion.