Try {(async function () {() a (). B. C ()}) ()} the catch (e) {the console. The log (` execution error: ${e.m essage} `)}Copy the code
What is the meaning of this passage?
- What happens when you execute an undefined function
- What happens if an error occurs inside async
- Try catch can only catch exceptions in synchronized code
So the answer is clear.
Because we’re executing an undefined function, we get an error a is not defind, and because we’re in async, we get an error message that says in Promise. Finally, try Cathch can only catch throws of synchronous code, and because async is async, it can’t go into a catch.
We can do it if we change the code like this:
Try {await (async function () {() a (). B. C ()}) ()} the catch (e) {the console. The log (` execution error: ${e.m essage} `)}Copy the code
As an additional tidbit, the latest Chrome already supports “await” in global scope.