生成器的异步/等待和ES6收益之间的区别 [英] Difference between async/await and ES6 yield with generators

查看:132
本文介绍了生成器的异步/等待和ES6收益之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在阅读这篇奇妙的文章« Generators »,它清楚地突出了此功能,这是一个用于处理生成器功能的辅助函数:

I was just reading this fantastic article «Generators» and it clearly highlights this function, which is a helper function for handling generator functions:

function async(makeGenerator){
  return function () {
    var generator = makeGenerator.apply(this, arguments);

    function handle(result){
      // result => { done: [Boolean], value: [Object] }
      if (result.done) return Promise.resolve(result.value);

      return Promise.resolve(result.value).then(function (res){
        return handle(generator.next(res));
      }, function (err){
        return handle(generator.throw(err));
      });
    }

    try {
      return handle(generator.next());
    } catch (ex) {
      return Promise.reject(ex);
    }
  }
}

我假设的

async/await实施async关键字的方式差不多.问题是,如果是这种情况,那到底是什么呢? await关键字和yield关键字之间有什么区别? await是否总是将某些东西变成承诺,而yield没有做出这样的保证?这是我最好的猜测!

which I hypothesize is more or less the way the async keyword is implemented with async/await. So the question is, if that is the case, then what the heck is the difference between the await keyword and the yield keyword? Does await always turn something into a promise, whereas yield makes no such guarantee? That is my best guess!

您还可以在本文中看到async/await与生成器的yield有何相似之处,其中他描述了"spawn"函数

You can also see how async/await is similar to yield with generators in this article where he describes the 'spawn' function ES7 async functions.

推荐答案

好吧,事实证明async/await与生成器之间存在非常密切的关系.而且我相信async/await将始终建立在生成器上.如果您查看Babel转换async/await的方式:

Well, it turns out that there is a very close relationship between async/await and generators. And I believe async/await will always be built on generators. If you look at the way Babel transpiles async/await:

巴别塔拿这个:

this.it('is a test', async function () {

    const foo = await 3;
    const bar = await new Promise(resolve => resolve('7'));
    const baz = bar * foo;
    console.log(baz);

});

并将其转换为此

function _asyncToGenerator(fn) {
    return function () {
        var gen = fn.apply(this, arguments);
        return new Promise(function (resolve, reject) {
            function step(key, arg) {
                try {
                    var info = gen[key](arg);
                    var value = info.value;
                } catch (error) {
                    reject(error);
                    return;
                }
                if (info.done) {
                    resolve(value);
                } else {
                    return Promise.resolve(value).then(function (value) {
                        return step("next", value);
                    }, function (err) {
                        return step("throw", err);
                    });
                }
            }

            return step("next");
        });
    };
}


this.it('is a test', _asyncToGenerator(function* () {   // << now it's a generator

    const foo = yield 3;    //  <<< now it's yield, not await
    const bar = yield new Promise(resolve => resolve(7));
    const baz = bar * foo;
    console.log(baz);

}));

你做数学.

这看起来像async关键字就是那个包装函数,但是如果是这种情况,那么await就变成了yield,稍后当它们出现时,图片的内容可能会更多成为本地人.

This makes it look like the async keyword is just that wrapper function, but if that's the case then await just gets turned into yield, there will probably be a bit more to the picture later on when they become native.

您可以在此处查看更多说明: https://www.promisejs.org/generators/

You can see more of an explanation for this here: https://www.promisejs.org/generators/

这篇关于生成器的异步/等待和ES6收益之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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