Blocking and non blocking calls to the ES0en. js callback function

  • 2020-10-23 20:52:32
  • OfStack

First, node. js, as the javascript running platform, adopts an event-driven and asynchronous programming approach. With event registration and asynchronous functions, developers can improve resource utilization and server performance. . Secondly, for the front people, node js as js platform, we can write the system level or server-side code to node javascript. js to perform, let's front man can also be applied to the background, by contrast, the browser javascript code at run time is limited by a variety of security, the operation of the system is limited, to the customer and node. js is a comprehensive background, provides javascript can realize many functions of many other languages.

Let's get back to business by first introducing blocking calls, which you'll see below.

1. Blocking call (read the file before performing the following operation)


var fs = require("fs");
var data = fs.readFileSync('/fs.txt');
console.log(data.toString());
console.log(" End of program execution !");

Output results:

"File Contents"

"Program execution over!"

2. Non-blocking calls (read files and perform other operations simultaneously)


var fs = require("fs"); 
fs.readFile('/fs.txt',function(err,data){
if(err) return console.error(err);
console.log(data.toString());
});
console.log(" End of program execution !");

Output results:

"Program execution over!"

"File Contents"

That's all the blocking and non-blocking calls from the node.js callback that this site has shown you, and I hope you enjoyed it.


Related articles: