问题
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.
回答1:
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 …()
.
回答2:
There's no urge to migrate from one to another because async
functions and co library can coexist in peace.
async
functions can be used inside co
generator functions, they are just promise-returning functions:
co.wrap(function* () {
yield asyncFn(1);
})()
.catch(console.error);
Generator functions can be used inside async
functions:
(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?
Considering that the generators are used in the app only in conjunction with co
, they can be replaced in automatic manner. function*
and *
methods are replaced with async
counterparts, yield
and yield*
are replaced with await
.
Before this can be done, some preliminary refactoring should be made. Only promises and generators should be used from this list of yieldables. Parallel execution (arrays and objects) should be replaced with respective Promise.all
:
const results = yield [...];
to
const results = yield Promise.all([...]);
回答3:
In case some one needs more information regarding migrating from co to async function, here is a detailed article about migration: https://medium.com/@nivekz/migrate-from-co-to-async-functions-4635d32d12bf
来源:https://stackoverflow.com/questions/41579282/migrating-from-generators-to-async-await