[英]Use jQuery or Q.Js for promises
I'm looking into BreezeJs and there samples are using Q.js for promises to handle asynchronous calls. 我正在研究BreezeJs,并且有样本正在使用Q.js来处理异步调用。 John Papa is also using Q. JQuery has promises as well . John Papa也在使用Q.JQuery也有承诺 。 What are the differences between the two? 两者有什么不同?
Both are based on the Promises/A standard and implement a then
method (though only current jQuery, they once had a incompatible pipe
instead of then
). 两者都基于Promises / A标准并实现了一个then
方法(尽管只有当前的jQuery,它们曾经有一个不兼容的pipe
而不是then
)。 However, there are a few differences: 但是,存在一些差异:
then
callbacks will be caught and reject the promise (and will only get re-thrown if you call .end()
). 将捕获异步then
回调中的所有抛出错误并拒绝承诺(并且只有在调用.end()
时才会重新抛出)。 Not sure whether I personally like that. 不确定我个人是否喜欢这样。 It's the standardized way which jQuery does not follow, rejecting from then
in jQuery deferreds is much more complicated . 这是该jQuery的不遵循,标准化的方式从拒绝then
在jQuery的deferreds要复杂得多 。 then
), while jQuery allows multiple arguments in resolve
/ reject
calls on its Deferreds. Q promise用单个值/原因解决(就像你then
返回/抛出它),而jQuery允许在其Deferreds上resolve
/ reject
调用中的多个参数。 .all
and similiar, which are more complicated with jQuery ( $.when.apply($, […])
). Q有.all
和类似,jQuery更复杂( $.when.apply($, […])
)。 … which is basically Promises/B . ......基本上是Promises / B. As you can see, the Q
API is more powerful, and (imho) better designed. 如您所见, Q
API更强大,并且(imho)设计得更好。 Depending on what you want to do, Q
could be the better choice, but maybe jQuery (especially if already included) is enough. 根据你想做的事情, Q
可能是更好的选择,但也许jQuery(特别是如果已经包含)就足够了。
JQuery's promise implementation of the Promises/A spec has some real issues. JQuery承诺实现Promises / A规范有一些实际问题。 The following link describes them far better than I can: missing-the-point-of-promises 以下链接比我更好地描述了它们: 遗漏了承诺点
Bergi's answer covers things fairly well. Bergi的答案很好地涵盖了事情。 I wanted to add, though, that we've created a guide for Q users coming from jQuery . 不过,我想补充说,我们已经为来自jQuery的Q用户创建了一个指南 。 To summarize the relevant sections: 总结相关部分:
then
/ pipe
. Q专注于链接所有方法,而jQuery只允许then
/ pipe
链接。 this
in which the caller must run). Q等同于不履行或拒绝一起跟踪上下文对象,因为这没有任何并行同步功能(即,你永远不返回一个值,以及一this
在调用者必须运行)。 So there is no resolveWith
or rejectWith
. 所以没有resolveWith
或rejectWith
。 The guide also contains a table paralleling jQuery and Q promise APIs. 该指南还包含一个与jQuery和Q promise API并行的表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.