从生成器迁移到 Async/Await
Migrating from Generators to Async/Await
我刚刚痛苦地意识到生成器函数不能与 await 一起使用。仅承诺或异步功能。
我的团队构建了一个完整的应用程序,所有模块都由生成器函数组成,其中一个是从主 js 文件调用 Co 模块。
除了遍历数百个生成器函数并将它们从 function*(...){
更改为 async function(...){
之外,还可以如何使生成器与 async/await 一起工作?
没有意义,因为 yield*/generators 和 async/await 在处理流的方式上非常相似,所以我想知道他们是如何错过等待支持生成器的。
你必须检查你的代码库并更改它,是的(当然你可能 write/use 一个为你做一切的工具)。
但是如果你愿意,你可以逐渐做到:用 async function
替换 function*
,在其中每个 yield
替换 await
,每个 yield*
通过 await co(…)
,然后将对前生成器函数的每次调用从 co(…)
更改为 …()
。
没有从一个迁移到另一个的冲动,因为 async
功能和 co
library 可以和平共存。
async
函数可以在 co
生成器函数中使用,它们只是返回承诺的函数:
co.wrap(function* () {
yield asyncFn(1);
})()
.catch(console.error);
生成器函数可以在 async
个函数中使用:
(async function () {
await co(genFn(1));
// for generator functions with no arguments, can also be
await co(genFn);
})()
.catch(console.error);
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?
考虑到生成器在应用程序中仅与 co
结合使用,它们可以自动替换。 function*
和 *
方法替换为 async
对应方法,yield
和 yield*
替换为 await
。
在此之前,应该进行一些初步的重构。 this list of yieldables 中只应使用 promises 和 generators。并行执行(数组和对象)应分别替换为 Promise.all
:
const results = yield [...];
至
const results = yield Promise.all([...]);
如果有人需要更多关于从 co 函数迁移到异步函数的信息,这里有一篇关于迁移的详细文章:https://medium.com/@nivekz/migrate-from-co-to-async-functions-4635d32d12bf
我刚刚痛苦地意识到生成器函数不能与 await 一起使用。仅承诺或异步功能。
我的团队构建了一个完整的应用程序,所有模块都由生成器函数组成,其中一个是从主 js 文件调用 Co 模块。
除了遍历数百个生成器函数并将它们从 function*(...){
更改为 async function(...){
之外,还可以如何使生成器与 async/await 一起工作?
没有意义,因为 yield*/generators 和 async/await 在处理流的方式上非常相似,所以我想知道他们是如何错过等待支持生成器的。
你必须检查你的代码库并更改它,是的(当然你可能 write/use 一个为你做一切的工具)。
但是如果你愿意,你可以逐渐做到:用 async function
替换 function*
,在其中每个 yield
替换 await
,每个 yield*
通过 await co(…)
,然后将对前生成器函数的每次调用从 co(…)
更改为 …()
。
没有从一个迁移到另一个的冲动,因为 async
功能和 co
library 可以和平共存。
async
函数可以在 co
生成器函数中使用,它们只是返回承诺的函数:
co.wrap(function* () {
yield asyncFn(1);
})()
.catch(console.error);
生成器函数可以在 async
个函数中使用:
(async function () {
await co(genFn(1));
// for generator functions with no arguments, can also be
await co(genFn);
})()
.catch(console.error);
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?
考虑到生成器在应用程序中仅与 co
结合使用,它们可以自动替换。 function*
和 *
方法替换为 async
对应方法,yield
和 yield*
替换为 await
。
在此之前,应该进行一些初步的重构。 this list of yieldables 中只应使用 promises 和 generators。并行执行(数组和对象)应分别替换为 Promise.all
:
const results = yield [...];
至
const results = yield Promise.all([...]);
如果有人需要更多关于从 co 函数迁移到异步函数的信息,这里有一篇关于迁移的详细文章:https://medium.com/@nivekz/migrate-from-co-to-async-functions-4635d32d12bf