使用 jQuery 或 Q.Js 进行 promise [英] Use jQuery or Q.Js for promises

查看:14
本文介绍了使用 jQuery 或 Q.Js 进行 promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 BreezeJs 并且有示例正在使用 Q.js 用于承诺处理异步调用.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 标准 并实现了一个 then 方法(虽然只是当前的 jQuery,他们曾经有一个不兼容的 pipe 而不是 then).但是,有一些区别:

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 有异常处理.所有在异步 then 回调中抛出的错误都将被捕获并拒绝承诺(并且只有在调用 .end() 时才会被重新抛出).不确定我个人是否喜欢那样.这是 jQuery 不遵循的标准化方式,在 jQuery deferreds 中从 then 拒绝要复杂得多.
  • Q 承诺使用单个值/原因解析(就像您从 then 返回/抛出它一样),而 jQuery 允许在 resolve/reject 中使用多个参数 调用它的 Deferred.
  • Q 有很多 代理方法,可以让你修改未来价值
  • Q 有 .all 和类似的,使用 jQuery ($.when.apply($, […])) 更复杂.
  • Q 确实在事件循环中明确地处理滴答并保证异步,而 jQuery 也可以是同步的.Promises 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 from then 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 in resolve/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.

...基本上是Promises/B.如您所见,Q API 更强大,(恕我直言)设计得更好.取决于你想要做什么,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 进行 promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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