Skip to content

关于异步编程 #3

@jpuncle

Description

@jpuncle

方案

  • 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 对象

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions