Blocking call and non-blocking call of node. js callback function _ node. js-js tutorial

Source: Internet
Author: User
This article focuses on introducing node. blocking and non-blocking calls of js callback functions involve node. knowledge about js callback functions. If you are interested in this article, learn it together. First, node. as a javascript running platform, js adopts event-driven and asynchronous programming methods. Through event registration and asynchronous functions, developers can improve resource utilization and server performance. Secondly, for front-end users, node. as a js running platform, js can be handed over to node by writing system-level or server-side javascript code. javascript Execution allows our front-end users to act on the background. In contrast, javascript code on the browser side is subject to various security restrictions during runtime, and the operations on the client system are limited, while node. javascript is a comprehensive background runtime, providing many functions that can be implemented by many other languages for javascript.

Next, let's get back to the question. First, let's introduce blocking calls. For more information, see.

1. Blocking call (read the file and then perform subsequent operations)

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

Output result:

"File content"

"Program execution is finished !"

2. Non-blocking call (synchronous execution of reading files and other operations)

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

Output result:

"Program execution is finished !"

"File content"

The above content is all the contents of blocking and non-blocking calls of the node. js callback function introduced by the editor. I hope you will like it.

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.