Yesterday, a little pig asked me about async/await. He said that he couldn't get the desired result no matter how he adjusted his head. When he adjusted his head and
turned his head, he turned his head and took a look. As expected , the layer by layer is simply better than Russia. Return a doll
But as long as you calm down and look at the following features slowly, async/await is still well understood
- Promise object does not need to be called with ()
- When instantiating Promise, the first function passed in will be executed immediately
var _promise = new Promise(function() {
/* 这里直接就执行了 */ })
- Async function is still a function, and it is a Promise object after being executed with ()
- (await _promise) is the content returned when _promise reports resolve
- await can only be used in async functions
- Await followed by a promise object will block the code until the promise is resolved; it will not be processed if it is not a promise object
- No matter what the code followed by await is, it will be executed immediately. When it is a promise object, the code behind is blocked
- When the promise followed by await reports reject, the code reports an error
- When _promise reports resolve, await _promise is similar to _promise.then()
console.log(await _promise)
_promise.then(function(params) {
console.log(params)
})
- When _promise reports reject, await _promise reports an error, combined with try…catch and _promise.catch() are almost the same
try {
await _promise
} catch(error) {
console.log(error)
}
_promise.catch(function(params) {
console.log(params)
})
end