Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between synchronous and asynchronous node

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "what is the difference between node synchronization and async", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the differences between node synchronization and asynchrony".

The difference between node synchronization and asynchronism is that synchronization is the top-down operation of the program, and the next step can be executed only after the previous step is executed; while async means that you do not have to wait for the above operation to run before running the following operation. Asynchronous programming can be achieved by relying on callback, but the program is either asynchronous after callback.

Operating environment of this tutorial: windows10 system, nodejs version 12.19.0, Dell G3 computer.

What is the difference between synchronous and asynchronous in node

Synchronization means that the program runs from top to bottom, while async means that you don't have to wait for the above to run before running the following operations. Asynchronous programming relies on callbacks, but it can't be said that the program is asynchronized after using callbacks.

Synchronized English: sync (synchronization)

Asynchronous English: async (asynchronous)

Synchronous API: you can proceed to the next API only after the current API execution is complete.

Console.log ('before'); console.log (' after')

Asynchronous API: the execution of the current API does not block the execution of subsequent code

Console.log ('before'); setTimeout (() = > {console.log (' last');}, 2000); console.log ('after')

The difference between synchronous API and asynchronous API (get the return value)

Synchronous API can get the result of API execution from the return value, but asynchronous API is not allowed (as if you can't get the result by writing return in asynchronous API? )

/ / Asynchronous function getMsg () {setTimeout (function () {return {msg: 'Hello Node.js'}}, 2000);} const msg = getMsg (); / / function does not write return returns undefinedcnsole.log (msg) by default; / / the output is undefined, because the timer executes the output before it is finished.

Callback function

Define your own functions for others to call.

/ / getData function definition function getData (callback) {} / / getData function call getData (() = > {}); / * example-* / function getMsg (callback) {setTimeout (function () {callback ({msg: 'Hello Node.js'})}, 2000);} getMsg (function (msg) {console.log (msg);})

The difference between synchronous API and asynchronous API (code execution order)

The synchronous API is executed from top to bottom, and the previous code blocks the execution of the later code.

Asynchronous API does not wait for API execution to complete before executing code down

Code execution sequence analysis

JavaScript is divided into synchronous code execution area and asynchronous code execution area, between which there is a callback function queue connection. First of all, JavaScript will execute all the contents of the synchronous code area, and then go to the asynchronous code area to execute the code to find the completed code block in the asynchronous code area. If you find it, immediately find the callback function corresponding to this asynchronous code block and put it in the synchronous code execution area to execute.

Asynchronous API in Node.js

Read the file API, there is a callback function.

The API of the event listener also has a callback function. (the event handler is the callback function, and the event listener API is asynchronous API)

How to solve this problem if the execution of the code behind the asynchronous API depends on the execution result of the current asynchronous API, but in fact the asynchronous API does not return the result when the subsequent code is executed?

For example, there is a requirement: read A file, B file and C file in turn (read A, B and C, not at the same time)

If the above requirement is implemented, the operation of reading B will be put into the callback function of A, while the operation of reading C will be put into the callback function of B, resulting in a lot of nesting levels (callback hell).

Const fs = require ('fs'); fs.readFile ('. / 1.txtforth, 'utf8', (err, result1) = > {console.log (result1) fs.readFile ('. / 2.txtforth, 'utf8', (err, result2) = > {console.log (result2) fs.readFile ('. / 3.txtforth, 'utf8', (err, result3) = > {console.log (result3)})) The above is about the content of this article on "what is the difference between synchronous and asynchronous in node". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report