Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion JS/JS-ch.md
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ console.log('1', a) // -> '1' 1
对于以上代码你可能会有疑惑,这里说明下原理

- 首先函数 `b` 先执行,在执行到 `await 10` 之前变量 `a` 还是 0,因为在 `await` 内部实现了 `generators` ,`generators` 会保留堆栈中东西,所以这时候 `a = 0` 被保存了下来
- 因为 `await` 是异步操作,所以会先执行 `console.log('1', a)`
- 因为 `await` 是异步操作,遇到`await`就会立即返回一个`pending`状态的`Promise`对象,暂时返回执行代码的控制权,使得函数外的代码得以继续执行,所以会先执行 `console.log('1', a)`
- 这时候同步代码执行完毕,开始执行异步代码,将保存下来的值拿出来使用,这时候 `a = 10`
- 然后后面就是常规执行代码了

Expand Down
2 changes: 1 addition & 1 deletion JS/JS-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ console.log('1', a) // -> '1' 1
You may have doubts about the above code, here we explain the principle:

- First the function `b` is executed. The variable `a` is still 0 before execution `await 10`, Because the `Generators` are implemented inside `await` and `Generators` will keep things in the stack, so at this time `a = 0` is saved
- Because `await` is an asynchronous operation, `console.log('1', a)` will be executed first.
- Because `await` is an asynchronous operation, it will immediately return a `pending` state `Promise` object when it encounter `await`, and temporarily returning control of the execution code, so that the code outside the function can continue to be executed. `console.log('1', a)` will be executed first.
- At this point, the synchronous code is completed and asynchronous code is started. The saved value is used. At this time, `a = 10`
- Then comes the usual code execution

Expand Down