何时应该使用yield和yield *与Koa.js [英] When should I use yield and when yield* with Koa.js

查看:231
本文介绍了何时应该使用yield和yield *与Koa.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览koa样本,文档和中间件时,我注意到这两种形式的 yield 正在被使用,没有任何特别的区别我注意到。最极端的情况是在koa-mount中,其中示例代码使用 yield next; 表单,包本身使用 yield * 几次。其他软件包(例如 koa-views )也使用 yield next form。

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.

我了解了语言定义的2种形式之间的区别,但不明白在koa的上下文中,它们是可互换使用的,何时使用另一个是正确的。

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.

经过一些更多的研究,我明白,由于koa建立在co之上,co能够处理多种类型的异步结果(thunk,Promises ...),这是可能的这两个都是合法,但我仍然不确定什么准则有助于决定在每个场景中使用哪种形式。

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.

推荐答案

本文 - 来自koa团队成员之一的产生下一个与收益*下一个,解释这是什么以及为什么使用它。

This article - yield next vs. yield* next , from one of koa's team members, explains exactly what this is and why they use it.

有些情况下可以使用这个 yield * ,如文章所示,以避免额外的$ code > co <​​/ code>调用,或保持上下文( this )。但是再一次,这不是真正需要的 - 正如作者所说,我们不主张避免混淆。

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".

这篇关于何时应该使用yield和yield *与Koa.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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