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

关于异步编程 #3

Open
jpuncle opened this issue Sep 9, 2017 · 0 comments
Open

关于异步编程 #3

jpuncle opened this issue Sep 9, 2017 · 0 comments

Comments

@jpuncle
Copy link
Owner

jpuncle commented Sep 9, 2017

方案

  • callback
  • event
  • pub/sub
  • Promise

借助 Generator 手动执行实现异步。需要自动执行的机制:

  • Thunk 函数,自动执行 Generator,参考 Thunkify 模块
  • co 模块, 自动执行 Generator,TJ 大神作品
co(function* gen() {
  yield Thunk 函数 // 回调中交回执行权
  yield Promise 函数 // then 中交回执行权
}).then(...)

async/await 实现异步编程

  • Generator 函数的语法糖
  • 自带执行器
  • 更好的语义
  • await 后面可以是 Promise 对象和原始类型的值
  • async 函数返回的是 Promise 对象
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