What is Callbacks Concept in Node JS

Page 1

What is Callbacks Concept in Node JS What is Callback? The recall is an asynchronous equivalent for a perform. A callback function is named at the completion of a given task. Node makes heavy use of callbacks. All APIs of Node are written in such how that they support callbacks. For model, a function to study a file may start analysis a file and return the control to the execution environment immediately in order that subsequent instruction is often executed. Once file I/O is complete, it'll call the callback function while passing the callback function, the content of the file as a parameter. So there's no blocking or await File I/O. This makes Node.js highly scalable because it can process the high number of requests without expecting any function to return the result.

Blocking Code Example: Create a js file named main.js which has the subsequent code: var fs = require("fs"); var data = fs.readFileSync('input.txt'); console.log(data.toString()); console.log("Program Ended"); Now run the main .js to ascertain the result: $ node main.js

Output:

Non-Blocking Code Example: Update main.js file to possess following code: var fs = require("fs"); var data = fs.readFileSync('input.txt'); console.log(data.toString()); console.log("Program Ended");


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.
What is Callbacks Concept in Node JS by Vishnu Priya - Issuu