Understanding of async/await, analysis of internal principles

The emergence of Promise solves the problem of callback hell, but if you encounter a complex business, the code will contain a large number of then functions, making the code still difficult to read.

For this reason, async/await was introduced in ES7, which is a major improvement of JavaScript asynchronous programming. It provides the ability to use synchronous code to access resources asynchronously without blocking the main thread , and makes the code logic clearer. It also supports try-catch to catch exceptions, which is very consistent with people's linear thinking.

Async/await, this way can completely bid farewell to executors and generators, and achieve more intuitive and concise code. According to the MDN definition, async is a function that is executed asynchronously and implicitly returns a Promise as a result. In fact, the async function is syntactic sugar for the generator function, and the generator function is improved. Its main point is that it has its own actuator, which is equivalent to encapsulating what we need to do extra (writing actuator/depending on co module) inside.

Guess you like

Origin blog.csdn.net/weixin_40599109/article/details/108452174