Blocking call and non-blocking call of node. js callback Functions
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.