使用jQuery或Q.Js进行承诺 [英] Use jQuery or Q.Js for promises
问题描述
我正在研究 BreezeJs ,并且样本正在使用 Q.js for promises处理异步调用。 John Papa 也在使用Q. JQuery也有承诺。这两者之间有什么区别?
I'm looking into BreezeJs and there samples are using Q.js for promises to handle asynchronous calls. John Papa is also using Q. JQuery has promises as well. What are the differences between the two?
推荐答案
两者都基于 Promises / A standard 并实现一个然后
方法(尽管只有当前的jQuery,它们曾经有一个不兼容的管道
而不是然后
)。但是,有一些区别:
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
). However, there are a few differences:
- Q有异常处理。将捕获异步
然后
回调中的所有抛出错误并拒绝承诺(并且只有在您调用.end()<时才会重新抛出/ code>)。不确定我个人是否喜欢这样。这是jQuery不遵循的标准化方式,在jQuery deferreds中拒绝
然后
更复杂的。 - Q承诺用一个值/原因解决(就像你从
那里返回/抛出它然后
),而jQuery允许中的多个参数解析
/拒绝
c $ c>对其Deferreds的调用。 - Q有很多代理方法,可以让你修改未来价值
- Q有
.all
和类似,jQuery更复杂($ .time.apply($,[...])
) 。 - Q确实在事件循环中使用ticks并保证异步,而jQuery也可以同步。现在需要承诺A / +规范。
- Q has exception handling. All thrown errors in the async
then
callbacks will be caught and reject the promise (and will only get re-thrown if you call.end()
). Not sure whether I personally like that. It's the standardized way which jQuery does not follow, rejecting fromthen
in jQuery deferreds is much more complicated. - Q promises are resolved with a single value/reason (like you return/throw it from
then
), while jQuery allows multiple arguments inresolve
/reject
calls on its Deferreds. - Q has lots of Proxy methods which will allow you to modifiy future values
- Q has
.all
and similiar, which are more complicated with jQuery ($.when.apply($, […])
). - Q does explicitly work with ticks in the event loop and guarantees asynchronity, while jQuery can be synchronous as well. This is now required by the Promises A/+ specification.
...这基本上是许/ B 。如您所见, Q
API更强大,并且(imho)设计得更好。根据你想要做的事情, Q
可能是更好的选择,但也许jQuery(特别是如果已经包含)就足够了。
… which is basically Promises/B. As you can see, the Q
API is more powerful, and (imho) better designed. Depending on what you want to do, Q
could be the better choice, but maybe jQuery (especially if already included) is enough.
这篇关于使用jQuery或Q.Js进行承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!