javascript 使用 jQuery 或 Q.Js 进行 promise

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13610741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:16:22  来源:igfitidea点击:

Use jQuery or Q.Js for promises

javascriptjquerybreezepromiseq

提问by Aligned

I'm looking into BreezeJsand there samples are using Q.jsfor promises to handle asynchronous calls. John Papais also using Q. JQuery has promises as well. What are the differences between the two?

我正在研究BreezeJs并且有示例使用Q.js来承诺处理异步调用。John Papa也在使用Q。JQuery 也有承诺。两者之间有什么区别?

回答by Bergi

Both are based on the Promises/A standardand implement a thenmethod (though only current jQuery, they once had a incompatible pipeinstead of then). However, there are a few differences:

两者都基于Promises/A 标准并实现了一个then方法(虽然只是当前的 jQuery,但它们曾经有一个不兼容的pipe而不是then)。但是,有一些区别:

  • Q has exception handling. All thrown errors in the async thencallbacks 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 thenin 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/rejectcalls on its Deferreds.
  • Q has lots of Proxy methodswhich will allow you to modifiy future values
  • Q has .alland 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.
  • Q 有异常处理。异步then回调中所有抛出的错误都将被捕获并拒绝承诺(并且只有在调用 时才会被重新抛出.end())。不确定我个人是否喜欢那样。这是 jQuery 不遵循的标准化方式,在 jQuery deferreds 中拒绝then要复杂得多
  • Q 承诺是用单个值/原因解决的(就像你从 返回/抛出它一样then),而 jQuery 允许在resolve/reject调用其 Deferred 上的多个参数。
  • Q 有很多Proxy 方法可以让你修改未来的值
  • Q has .alland similiar, jQuery ( $.when.apply($, […]))更复杂。
  • Q 确实在事件循环中明确处理滴答并保证异步,而 jQuery 也可以是同步的。Promises A/+ 规范现在要求这样做。

… which is basically Promises/B. As you can see, the QAPI is more powerful, and (imho) better designed. Depending on what you want to do, Qcould be the better choice, but maybe jQuery (especially if already included) is enough.

...这基本上是Promises/B。如您所见,QAPI 更强大,并且(恕我直言)设计得更好。根据您想要做什么,Q可能是更好的选择,但也许 jQuery(特别是如果已经包含)就足够了。

回答by Jay Traband

JQuery's promise implementation of the Promises/A spec has some real issues. The following link describes them far better than I can: missing-the-point-of-promises

JQuery 对 Promises/A 规范的承诺实现存在一些实际问题。以下链接比我能更好地描述它们:missing-the-point-of-promises

回答by Domenic

Bergi's answer covers things fairly well. I wanted to add, though, that we've created a guide for Q users coming from jQuery. To summarize the relevant sections:

贝尔吉的回答涵盖了相当多的内容。不过,我想补充一点,我们已经为来自 jQuery 的 Q 用户创建了一个指南。总结相关部分:

  • Q handles exceptions, allowing you to handle all errors through a uniform interface.
  • Q focuses on chaining with all its methods, whereas jQuery only allows chaining from 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 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 enforces a separation between the deferred and the promise, whereas jQuery merges them into one object with the option of separating them.
  • Q does not track a context object along with the fulfillment or rejection, since this has no parallel for synchronous functions (i.e., you never return a value as well as a thisin which the caller must run). So there is no resolveWithor rejectWith.
  • Q uses Promises/A+ terminology; the main difference is that Q uses "fulfilled" where jQuery uses "resolved," and in Q "resolved" means something more subtle.
  • Q 处理异常,允许您通过统一接口处理所有错误。
  • Q 专注于链接其所有方法,而 jQuery 只允许从then/链接pipe
  • Q 承诺保证异步性,从而避免由 jQuery 有时同步,有时异步行为导致的控制流危险和竞争条件。
  • Q 承诺总是用单个值实现或用单个原因拒绝,就像同步函数总是返回单个值或抛出单个异常一样。
  • Q 强制将 deferred 和 promise 分开,而 jQuery 将它们合并为一个对象,并可选择将它们分开。
  • Q 不跟踪上下文对象以及实现或拒绝,因为这对于同步函数没有并行(即,您永远不会返回一个值以及this调用者必须在其中运行的值)。所以没有resolveWithrejectWith
  • Q 使用 Promises/A+ 术语;主要区别在于 Q 使用“已实现”,而 jQuery 使用“已解决”,而在 Q 中“已解决”意味着更微妙的东西。

The guide also contains a table paralleling jQuery and Q promise APIs.

该指南还包含一个与 jQuery 和 Q promise API 并行的表。