all-of-frontend/Answer/11-20/17.md
2021-03-31 07:42:54 +08:00

14 lines
480 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

```js
var a = 0
var b = async () => {
a = a + await 10
console.log('2', a) // ->
}
b()
a++
console.log('1', a) // ->
```
这道题目大部分读者肯定会想到 `await` 左边是异步代码,因此会先把同步代码执行完,此时 `a` 已经变成 1所以答案应该是 11。
其实 `a` 为 0 是因为加法运算法,先算左边再算右边,所以会把 0 固定下来。如果我们把题目改成 `await 10 + a` 的话,答案就是 11 了。