简体   繁体   English

使用jQuery或Q.Js进行承诺

[英]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: 但是,存在一些差异:

  • Q has exception handling. Q有异常处理。 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() ). 将捕获异步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要复杂得多
  • 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 promise用单个值/原因解决(就像你then返回/抛出它),而jQuery允许在其Deferreds上resolve / reject调用中的多个参数。
  • Q has lots of Proxy methods which will allow you to modifiy future values Q有许多代理方法 ,可以让你修改未来的值
  • Q has .all and similiar, which are more complicated with jQuery ( $.when.apply($, […]) ). Q有.all和类似,jQuery更复杂( $.when.apply($, […]) )。
  • Q does explicitly work with ticks in the event loop and guarantees asynchronity, while jQuery can be synchronous as well. Q确实在事件循环中使用ticks并保证异步,而jQuery也可以是同步的。 This is now required by the Promises A/+ specification . 现在Promise A / +规范要求这样做

… 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: 总结相关部分:

  • Q handles exceptions, allowing you to handle all errors through a uniform interface. Q处理异常,允许您通过统一接口处理所有错误。
  • Q focuses on chaining with all its methods, whereas jQuery only allows chaining from then / pipe . Q专注于链接所有方法,而jQuery只允许then / pipe链接。
  • Q promises guarantee asynchronicity, thus avoiding the control flow hazards and race conditions that result from jQuery's sometimes-sync, sometimes-async behavior. Q承诺保证异步性,从而避免由于jQuery有时同步,有时是异步行为而导致的控制流危险和竞争条件。
  • Q promises are always fulfilled with a single value or rejected with a single reason, just like synchronous functions always either return a single value or throw a single exception. Q promises总是用单个值来实现,或者由于一个原因而被拒绝,就像同步函数总是返回单个值或抛出一个异常一样。
  • Q enforces a separation between the deferred and the promise, whereas jQuery merges them into one object with the option of separating them. Q强制延迟和promise之间的分离,而jQuery将它们合并为一个对象,并可以选择将它们分开。
  • Q does not track a context object along with the fulfillment or rejection, since this has no parallel for synchronous functions (ie, you never return a value as well as a this in which the caller must run). Q等同于不履行或拒绝一起跟踪上下文对象,因为这没有任何并行同步功能(即,你永远不返回一个值,以及一this在调用者必须运行)。 So there is no resolveWith or rejectWith . 所以没有resolveWithrejectWith
  • Q uses Promises/A+ terminology; Q使用Promises / A +术语; the main difference is that Q uses "fulfilled" where jQuery uses "resolved," and in Q "resolved" means something more subtle. 主要区别在于Q使用“履行”,jQuery使用“已解决”,而Q“解决”则意味着更微妙的东西。

The guide also contains a table paralleling jQuery and Q promise APIs. 该指南还包含一个与jQuery和Q promise API并行的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM