Detailed explanation of the difference between await and then

The biggest difference between Async/Await and Promise is: await b() will suspend the execution of the async function in which it is located; and Promise.then(b) will continue to execute the current function after adding the b function to the callback chain. For the stack, this difference is very critical.
The following is the request function encapsulated by promise.
Insert picture description here
The following is a data request using await and then.
Insert picture description here

The way of await:

After executing the 40th line of code, the execution of the code after the 40th line will be suspended, and the execution of the following code will not start until the requested data is returned. So the data in the next 41 rows is the data requested.

the way of then

After the request is sent, the content in then will be put on the stack, and the code after then will be executed in normal order, so when the 51st line is executed, the data will be output undefine. When the requested data comes back, the code segment in then (46-48 lines of code) is executed.

Personal understanding, welcome to criticize and correct any mistakes!

Guess you like

Origin blog.csdn.net/qq_44606064/article/details/113796423