Koa / Co / Bluebird or Q / Generators / Promises / Thunks 的相互关系

经常游荡在 SO 的我总能发现许多好问题和好答案。它们的“好”不仅仅在于知识的价值,更可贵之处在于如何表达:如何“提问”/如何“回答”。不久前我在 SF 发了一篇 WebComponents 和 React 的对比 就是一个很典型的范例,今天我又发现看到这样的一篇,不敢独享,略做翻译继续奉献给诸位。(唯一的回答比较长还没翻译完且我也在等待更好的回答,等有时间会补充)

Koa / Co / Bluebird or Q / Generators / Promises / Thunks interplay? (Node.js)

Koa / Co / Bluebird or Q / Generators / Promises / Thunks 的相互关系(影响/作用)?

I'm investigating building a web app in part with Koa, but I don't quite have a handle on the hows, whens, and whys of choosing between - and applying - the range of supportive "making async easier" technologies/approaches (listed below).
我正在为了创建 web 应用时和 Koa 有关的部分做调研,但我对于一系列“让异步编程更轻松”的技术/方法(下文列出),很难抉择如何、何时、以及为何选择且运用。

Overall the disparate guidance on the web about this subject still leaves things blurry, especially in respect to evolving best practices, or at least better ones, and under what scenarios. There seems to be little or nothing on the web that puts it all in context.
尽管网络上有着各式各样的关于该主题的指导,但涉及到在各种情景下的最佳实践,或者较好实践的方面还是不够清晰。看起来网上几乎没有“百科全书”。

I'm hoping the responses to this big arse sprawling post can correct that. Also maybe the questions below can inspire someone to write a thorough blog post or the like to address this matter. My sense is I'm not even close to the only one who would benefit from that.
我寄希望于针对我这篇“裹脚布”式问题的回复可以改变这一现状。同时也希望下面的问题可以激发某人写一篇博客或什么的将其囊括总结。我感觉因此收益的绝对不止是我一个人。

So I'd be pleased if the bright community could help answer and provide clarity to the following questions in respect to the technologies listed below (in bold type):
所以我很高兴若有人能从如下角度针对后面的问题(黑体)提供清楚的回答:

a) How, and under what circumstance (as applicable) are they complements, supplements, substitutes, and/or overlapping solutions to one another?
a) 在何种(可适用的)情形下,它们相互之间可怎样补充、替换、并且/或者彼此重复?

b) What are their trade-offs in respect to speed-performance, error handling ease, and debugging ease?
b) 在速度/性能,便于错误处理和便于调试这几方面,它们各自的利弊是什么?

c) When, where, and why may it be better to use "this" versus "that" technology, technologies-combo, and/or approach?
c) 在何时,何处用“这个”代替“那个”技术/技术组合/方法会更好,以及原因为何?

d) Which technologies or approaches, if any, may be "dimming stars".
d) 哪一种技术或方法,如果有的话,可能已经是“昨日黄花”。


Technologies:
技术:

Koa

My understanding:
我的理解:

Koa is a minimal foundation for build Node apps geared for taking advantage of ECMAScript-6 features, one feature in particular being generators.
Koa 是受益于 ES6特性,特别是 Generators 特性的用来开发 Node 应用的最小化基础框架。

Co

My understanding:
我的理解:

  • Co is a library of utilites for running ECMAScript-6 generators (which are native to Node .011 harmony), with the goal to allieve some/much(?) of the need to write boilerplate code for running and managing generators.
    Co 是运行 ES6 Generators(Node .011 harmony 模式下原生支持)的工具类库,其目标是帮助编写部分/更多(?)的用于运行和管理 generators 的模版代码。

  • Co is intrinsically part of Koa(?).
    Co 本质上是 Koa 的一部分(?)。

Specific questions:
针对性问题:

  • If and how does one use Co differently in Koa than in a non-Koa context. In other words, does Koa wholly facade Co?
    能否且若能则如何让 Co 在非 Koa 的场合下以不同于 Koa 的方式使用?换言之,Koa 是否只是换了一个“马甲”的 Co?

  • Could Co be replaced in Koa with some other like generator library if there is/was a better one? Are there any?
    在 Koa 当中有能代替 Co 的其他 generator 类库吗?

Promise Libraries such as "Q" and Bluebird

My understanding:
我的理解:

  • They are in a sense "polyfills" for implmententing the Promises/A+ spec, if and until Node natively runs that spec.
    它们都是 Promises/A+ 规范的 polyfills 实现,当且直到 Node 原生实现了该规范。

  • They have some further non-spec convenience utilities for facilitating the use promises, such as Bluebird's promisfyAll utility.
    它们实现了一些规范外的便利方法以便更好的使用 promises,比如 Bluebird 的 promisfyAll

Specific questions:
针对性问题:

  • My understanding is the ECMAScript-6 spec does/will largely reflect the Promises/A+ spec, but even so, Node 0.11v harmony does not natively implement Promises. (Is this correct?) However when it does, will technologies such as Q and Bluebird be on their way out?
    我理解中的 ES6 规范(将)会大量体现 Promises/A+ 规范,可即便如此,Node 0.11v harmony 没有原生 Promises 实现(对吗?)。若将来实现了,像 Q 和 Bluebird 这样的技术是不是就要过时了?

  • I've read something to the effect that "Q" and Bluebird support generators. What does this mean? Does it mean in part that, for example, they to some degree provided the same utility as Co, and if so to what degree?
    我读到过一些东西说 Q 和 Bluebird 支持 generators。这是什么意思?是不是意味着在某种程度上它们提供了和 Co 一样的功能?如果是的化,一样到了何种程度?

Thunks and Promises
(译注:此二者皆为术语不做翻译更佳)

I think I have an fair handle on what they are, but hoping someone can provide a succinct and clear "elevator pitch" definition on what each is, and of course, as asked above, to explain when to use one versus the other -- in a Koa context and not in it.
我想我对此二者已经理解了,不过还是希望有谁能提供一个简明扼要的“电梯推介”(译注:即在做个电梯的时间里,如一分钟内,说清楚一个概念),且能解释下二者的对比——在使用和不使用 Koa 的情形下。

Specific questions:
针对性问题:

  • Pro and cons to using something like Bluebird's promisfy, versus say using Thunkify (github com/visionmedia/node-thunkify)?
    对比使用 Bluebird 等 promise 式的库和 thunk 式的库(如 https://github.com/visionmedia/node-thunkify)之间的优点和缺点

To give some further context to this post and its questions, it might be interesting if Koa techniques presented in the following webpages could be discussed and contrasted (especiallly on a pros vs cons basis):
为了对本问题提供更多的背景资料,下列涉及 Koa 技术的资料若能一并加以探讨和对比(特别是优劣比较)那就再好不过了:

Thanks all!


太极客(Very Geek)
As a designeer, I hope you can prove me wrong.

正在更新 Elixir 语言的系列文章:[链接]

31.1k 声望
3.1k 粉丝
0 条评论
推荐阅读
为 Koa 框架封装 webpack-dev-middleware 中间件
我见到有很多朋友在 SegmentFault 上面问一些不太好回答的问题,“JavaScript/Node 学好了能做什么?”,“前端架构师每天都做些什么?”等等。这些问题并非不能回答,但是第一、问题本身太过泛泛,很难回答的既针对...

n͛i͛g͛h͛t͛i͛r͛e͛25阅读 12.6k评论 6

安全地在前后端之间传输数据 - 「3」真的安全吗?
在「2」注册和登录示例中,我们通过非对称加密算法实现了浏览器和 Web 服务器之间的安全传输。看起来一切都很美好,但是危险就在哪里,有些人发现了,有些人嗅到了,更多人却浑然不知。就像是给门上了把好锁,还...

边城31阅读 7.1k评论 5

封面图
在前端使用 JS 进行分类汇总
最近遇到一些同学在问 JS 中进行数据统计的问题。虽然数据统计一般会在数据库中进行,但是后端遇到需要使用程序来进行统计的情况也非常多。.NET 就为了对内存数据和数据库数据进行统一地数据处理,发明了 LINQ (L...

边城17阅读 1.8k

封面图
【已结束】SegmentFault 思否写作挑战赛!
SegmentFault 思否写作挑战赛 是思否社区新上线的系列社区活动在 2 月 8 日 正式面向社区所有用户开启;挑战赛中包含多个可供作者选择的热门技术方向,根据挑战难度分为多个等级,快来参与挑战,向更好的自己前进!

SegmentFault思否20阅读 5.5k评论 10

封面图
过滤/筛选树节点
又是树,是我跟树杠上了吗?—— 不,是树的问题太多了!🔗 相关文章推荐:使用递归遍历并转换树形数据(以 TypeScript 为例)从列表生成树 (JavaScript/TypeScript) 过滤和筛选是一个意思,都是 filter。对于列表来...

边城18阅读 7.6k评论 3

封面图
涨姿势了,有意思的气泡 Loading 效果
今日,群友提问,如何实现这么一个 Loading 效果:这个确实有点意思,但是这是 CSS 能够完成的?没错,这个效果中的核心气泡效果,其实借助 CSS 中的滤镜,能够比较轻松的实现,就是所需的元素可能多点。参考我们...

chokcoco18阅读 2k评论 2

「彻底弄懂」this全面解析
当一个函数被调用时,会创建一个活动记录(有时候也称为执行上下文)。这个记录会包含函数在 哪里被调用(调用栈)、函数的调用方法、传入的参数等信息。this就是记录的其中一个属性,会在 函数执行的过程中用到...

wuwhs17阅读 2.3k

封面图

正在更新 Elixir 语言的系列文章:[链接]

31.1k 声望
3.1k 粉丝
宣传栏