When should I use yield and when yield* with Koa.js

半腔热情 提交于 2019-12-07 17:54:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!