Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

深入理解 async/await #33

Open
JTangming opened this issue Nov 2, 2019 · 1 comment
Open

深入理解 async/await #33

JTangming opened this issue Nov 2, 2019 · 1 comment

Comments

@JTangming
Copy link
Owner

JTangming commented Nov 2, 2019

理解 async/await

可以直接参考:理解 async/await

ES7 提出的 async 函数,终于让 JavaScript 对于异步操作有了终极解决方案(No more callback hell

Async 函数的改进在于下面四点:

  • 内置执行器 Generator 函数的执行必须依靠执行器,而 Aysnc 函数自带执行器,更直观的同步写法
  • 更好的语义 async 和 await 相较于 * 和 yield 更加语义化
  • 更广的适用性 async 函数的 await 命令后面则可以是 Promise 或者基本类型的值(Number,string,boolean...)
  • 返回值是 Promise async 函数返回值是 Promise 对象,可以直接使用 then() 方法进行调用
@JTangming JTangming changed the title 线性表 深入理解 async/await Feb 1, 2020
@JTangming
Copy link
Owner Author

JTangming commented Feb 1, 2020

标准文档是把 async/await 作为一个 promise 语法糖,用 promise 封装的 generator 自动执行函数。

v8 按照标准文档,底层是用 promise 实现了async/await。

babel 是把 async/await 转换为 generators 和 promises,再通过 regenerator-runtime 把 generator 转换为 ES5 代码。

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant