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

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

问题描述

我对目前关于添加异步函数和关键字等待到下一个EcmaScript的讨论感到困惑。



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



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



等待应该简单可以在正常函数和生成器函数中使用,没有额外的 async 标记。



如果我需要创建一个函数什么应该可用作 await 的结果,我只是使用一个承诺。



我的询问理由这个很好的解释,以下示例来自:

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

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

//更多逻辑
}

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

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

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

//返回因为createUser()和getFacebookFriends()以及inviteFacebookFriends()结束了他们等待的结果。

}

在我看来,整个功能执行一直持续到下一个tick(等待履行)完成。与Generator-Function的不同之处在于next()正在触发并更改对象的值和完成字段。一个函数将简单地返回结果,并且触发器是一个函数内部触发器,如while循环。

解决方案

< blockquote>

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


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




  • * 表示一个生成器函数,该函数将始终返回一个可以从外部推进(和停止)的生成器,类似于迭代器。

  • async 表示一个异步函数,它将始终返回一个依赖于其他promise并且其执行并发的promise。其他异步操作(可能会从外部取消)。



关键字不是必需的,而且函数的种类也是如此可以通过它的正文中是否出现相应的关键字( yield(*) / 等待)来确定导致代码维护较少:




  • 不易理解,因为你需要扫描整个身体确定

  • 更多errorprone,因为通过添加/删除这些关键字而不会出现语法错误很容易打破函数




一个普通函数,执行将等待完成孔体,直到所有等待完成为止


听起来你想要一个阻止功能,这是一个 非常坏主意在并发设置中。


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

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 should simple be usable within normal functions and generator functions with no additional async marker.

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.

}

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.

解决方案

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

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.

  • 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).

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:

  • less comprehensible, because you need to scan the whole body to determine the kind
  • more errorprone, because it's easy to break a function by adding/removing those keywords without getting a syntax error

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天全站免登陆