从发电机迁移到异步/等待 [英] Migrating from Generators to Async/Await

查看:143
本文介绍了从发电机迁移到异步/等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚来到痛苦的认识,发电机功能不能等待使用。只有承诺或异步功能。



我的团队构建了一个包含发电机功能的所有模块的整个应用程序,从主js文件调用Co模块。 p>

除了通过数百个生成器函数并将它们从函数*(...){更改为异步函数(...){,发生器如何使用async / await呢?



没有意义,因为yield * / generator和async / await在处理流程方面非常相似,所以我想知道他们如何错过等待支持生成器。

解决方案

你必须经过你的代码库并改变它,是的(当然你可以写/使用一个可以为你做的一切工具)。



但是,如果需要,可以逐渐执行:将函数* 替换为异步函数,其中每个 yield by await and each yield * by await co(...),然后将每个来自 co(...)的调用变为 ...()


I just came to the painful realization that generator functions cannot be used with await. Only promises or async functions.

My team built an entire application with all modules consisting of generator functions, with one call to the Co module from the main js file.

Besides going though hundreds of generator function and changing them from function*(...){ to async function(...){, how else can generators be made to work with async/await?

Makes no sense because yield*/generators and async/await are pretty similar in how they handle flow so I'm wondering how they missed out on having await support generators.

解决方案

You have to go through your code base and change it, yes (of course you might write/use a tool that does everything for you).

But you can do it gradually if you want: Replace a function* by async function, inside it every yield by await and every yield* by await co(…), and then change every call to the former generator function from co(…) to …().

这篇关于从发电机迁移到异步/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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