Skip to content

Commit 8af0e15

Browse files
yygmindYuChengKai
authored andcommitted
await add new description
1 parent 9b73b0e commit 8af0e15

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

JS/JS-ch.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ console.log('1', a) // -> '1' 1
14261426
对于以上代码你可能会有疑惑,这里说明下原理
14271427
14281428
- 首先函数 `b` 先执行,在执行到 `await 10` 之前变量 `a` 还是 0,因为在 `await` 内部实现了 `generators``generators` 会保留堆栈中东西,所以这时候 `a = 0` 被保存了下来
1429-
- 因为 `await` 是异步操作,所以会先执行 `console.log('1', a)`
1429+
- 因为 `await` 是异步操作,遇到`await`就会立即返回一个`pending`状态的`Promise`对象,暂时返回执行代码的控制权,使得函数外的代码得以继续执行,所以会先执行 `console.log('1', a)`
14301430
- 这时候同步代码执行完毕,开始执行异步代码,将保存下来的值拿出来使用,这时候 `a = 10`
14311431
- 然后后面就是常规执行代码了
14321432

0 commit comments

Comments
 (0)