Blocking and non-blocking calls to Node.js callback functions _node.js

Source: Internet
Author: User

First of all, Node.js as a JavaScript platform, it uses event-driven and asynchronous programming, through event registration and asynchronous functions, developers can improve resource utilization, server performance can also be improved. Second, for the front end people, Node.js as a running platform for JS, we can write system-level or server-side JavaScript code to Node.js to execute, let us front-end people can also be used in the background, in contrast, browser-side JavaScript code in the runtime will be subject to a variety of security restrictions, the operation of the customer system has , while Node.js is a comprehensive background runtime that provides JavaScript with many of the features that many other languages can achieve.

The following return to the topic, first of all to introduce blocking calls, the specific content please look down.

1. Blocking calls (after reading the file and then performing the subsequent 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"

"End of program execution!" ”

2. Non-blocking calls (read files and other actions synchronously)

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:

"End of program execution!" ”

"File Contents"

The above content is small to introduce the Node.js callback function block call and non-blocking call all content, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.