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 #5

Closed
yongheng2016 opened this issue Mar 30, 2018 · 0 comments
Closed

async #5

yongheng2016 opened this issue Mar 30, 2018 · 0 comments

Comments

@yongheng2016
Copy link
Owner

yongheng2016 commented Mar 30, 2018

async函数就是将 Generator 函数的星号(*)替换成async,将yield替换成await,仅此而已。

async函数相对于Generator函数的改进:

  • 内置执行器

可以像普通函数一样执行

const asyncReadFile = async function () {
  const f1 = await readFile('/etc/fstab');
  const f2 = await readFile('/etc/shells');
  console.log(f1.toString());
  console.log(f2.toString());
};

asyncReadFile()  //可以像普通函数一样执行
  • 更好的语义

语义化更清楚了

  • 更广的适用性

co模块约定,yield命令后面只能是 Thunk 函数或 Promise 对象,而async函数的await命令后面,可以是 Promise 对象和原始类型的值(数值、字符串和布尔值,但这时等同于同步操作)。

  • 返回值是Promise

返回值是Promise对象,可以用then指定下一步操作

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