ES2017 - 异步与收益 [英] ES2017 - Async vs. Yield

查看:14
本文介绍了ES2017 - 异步与收益的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对当前关于向下一个 EcmaScript 添加异步函数和关键字 await 的讨论感到困惑.

I am confused about the current discussion of adding async functions and the keyword await to the next EcmaScript.

我不明白为什么必须在 function 关键字之前添加 async 关键字.

I do not understand why it is necessary to have the async keyword before the function keyword.

从我的角度来看,await 关键字等待生成器或 promise done 的结果,函数的 return 应该就足够了.

From my point of view the await keyword to wait for a result of a generator or promise done, a function's return should be enough.

await 应该很简单,可以在普通函数和生成器函数中使用,无需额外的 async 标记.

await should simple be usable within normal functions and generator functions with no additional async marker.

如果我需要创建一个函数,它应该可以用作 await 的结果,我只需使用一个承诺.

And if I need to create a function what should be usable as an result for an await, I simply use a promise.

我问的原因是这个很好的解释,下面的例子来自:

My reason for asking is this good explanation, where the following example comes from:

async function setupNewUser(name) {  
  var invitations,
      newUser = await createUser(name),
      friends = await getFacebookFriends(name);

  if (friends) {
    invitations = await inviteFacebookFriends(friends);
  }

  // some more logic
}

它也可以作为普通函数完成,如果函数的执行将等待完成洞函数,直到所有等待完成.

It also could be done as normal function, if the execution of a function will wait for finishing the hole function until all awaits are fulfilled.

function setupNewUser(name) {  
  var invitations,
      newUser = await createUser(name),
      friends = await getFacebookFriends(name);

  if (friends) {
    invitations = await inviteFacebookFriends(friends);
  }

  // return because createUser() and getFacebookFriends() and maybe inviteFacebookFriends() finished their awaited result.

}

在我看来,整个函数执行会一直保持到下一个滴答声(等待执行)完成.与 Generator-Function 的不同之处在于 next() 正在触发和更改对象的值和 done 字段.相反,一个函数会在完成时简单地返回结果,并且触发器是一个函数内部触发器,如 while 循环.

In my opinion the whole function execution is holding until the next tick (await fulfillment) is done. The difference to Generator-Function is that the next() is triggering and changing the object's value and done field. A function instead will simple give back the result when it is done and the trigger is a function internal trigger like a while-loop.

推荐答案

我不明白为什么必须在 function 关键字之前使用 async 关键字.

出于同样的原因,我们在生成器函数之前有 * 符号:它们将函数标记为非凡.它们在这方面非常相似——它们添加了一个视觉标记,表明该函数的主体不会自行运行完成,但可以与其他代码任意交错.

For the same reason that we have the * symbol before generator functions: They mark the function as extraordinary. They are quite similar in that regard - they add a visual marker that the body of this function does not run to completion by itself, but can be interleaved arbitrarily with other code.

  • * 表示一个生成器函数,它将始终返回一个生成器,该生成器可以通过使用类似于迭代器的方式从外部推进(和停止).
  • async 表示一个异步函数,它总是会返回一个依赖于其他 Promise 的 Promise,并且它的执行与其他异步操作是并发的(并且可能会从外部取消).
  • The * denotes a generator function, which will always return a generator that can be advanced (and stopped) from outside by consuming it similar to an iterator.
  • The async denotes an asynchronous function, which will always return a promise that depends on other promises and whose execution is concurrent to other asynchronous operations (and might be cancelled from outside).

确实,关键字并不是绝对必要的,函数的类型可以根据各个关键字(yield(*)/await)是否出现在它的主体,但这会导致代码的可维护性降低:

It's true that the keyword is not strictly necessary and the kind of the function could be determined by whether the respective keywords (yield(*)/await) appear in its body, but that would lead to less maintainable code:

  • 不太好理解,因为需要全身扫描才能确定种类
  • 更容易出错,因为通过添加/删除这些关键字很容易破坏函数而不会出现语法错误

一个正常的函数,它的执行将等待完成洞体直到所有的等待都完成

a normal function, whose execution will wait for finishing the hole body until all awaits are fulfilled

听起来你想要一个阻塞函数,这是一个非常坏主意并发设置.

That sounds like you want a blocking function, which is a very bad idea in a concurrent setting.

这篇关于ES2017 - 异步与收益的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆