问题
Browsing through koa samples, docs and middleware, I noticed both forms of yield
are being used without any particular difference i noticed. The most extreme case is in koa-mount, where the sample code uses the yield next;
form, and the package itself uses yield*
several times. Other packages (koa-views for example) also use the yield next
form.
I understand the difference between the 2 forms as defined by the language, but don't understand how is it that in the context of koa they're used interchangeably and when is it correct to use one over the other.
EDIT 29/5
After some more research, I understand that since koa is built on top of co, and co is able to process multiple types of asynchronous results (thunks, Promises...), it is possible that both are legal, but I'm still not sure what guideline helps decide which form to use in each scenario.
回答1:
This article - yield next vs. yield* next , from one of koa's team members, explains exactly what this is and why they use it.
There are a few cases where one could use this yield *
, as the articles shows, for avoiding extra co
calls, or keeping the context (this
) when yielding. But then again, it's not really needed - as the author states, "we don't advocate it to avoid confusion".
来源:https://stackoverflow.com/questions/30457200/when-should-i-use-yield-and-when-yield-with-koa-js