1 / 2

What is Callbacks Concept in Node JS

VisualPath Node JS training in Hyderabad will help you develop the skills required to build applications using Node JS. This training will enable you to gain in-depth knowledge of the fundamental concepts of Node.js and provides hands-on experience. All the class recordings, presentations will be shared with you for reference.Call Now 919989971070

Download Presentation

What is Callbacks Concept in Node JS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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");

  2. Now run the main .js to ascertain the result: $ node main.js Output These two examples justify the concept of blocking and non-blocking calls. the primary example shows that program blocks until it reads the file then only it proceeds to finish the program whereas within the second example, the program doesn't await file reading but it just proceeded to print "Program Ended" and at the same time program without blocking continues reading the file. Thus, a blocking program executes considerably in sequence and from the programming point of view it's easier to implement the logic but non-blocking programs don't execute in sequence, so just in case a program must use any data to be processed, it should be kept within an equivalent block to form it sequential execution. For More information about Node JS Visit our Website Call +919989971070

More Related