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

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

问题描述

我对当前关于添加异步函数和关键字 await 到下一个EcmaScript(假设EcmaScript 2016?)的讨论感到困惑。

I am confused about the current discussion of adding async functions and the keyword await to the next EcmaScript (assuming EcmaScript 2016?).

我不明白为什么在函数关键字之前必须有 async 关键字

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

从我的角度来看,等待关键字等待生成器或承诺的结果完成,函数的返回应该是足够的。

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 的结果应该可以使用,我只是使用承诺。

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()触发并更改对象的值和完成字段。一个函数反而会简单地给出结果,当它完成并且触发器是一个函数内部触发器,就像一个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.

希望有人可以澄清这是否是我的自己的事情混乱,或者是因为ES 7讨论的早期阶段。

Hope someone can clarify whether this is my own confusion of things or because it's because of the early stages of discussion about ES 7.

推荐答案


我不明白为什么在功能关键字之前需要有 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 表示一个异步函数,它将始终返回依赖于其他承诺的承诺,并且其执行是并发的其他异步操作(可能会从外部取消)。

  • 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(*) / 等待)出现在其正文中,但是导致较少的可维护代码:

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:


  • 不太容易理解,因为您需要扫描全身确定类型

  • 更多errorprone,因为通过添加/删除这些关键字而不会产生语法错误来轻松地打破一个功能


一个正常的功能,其执行将等待完成孔体直到所有等待满足

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.

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

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