Javascript Promise vs jQuery Deferred [英] Javascript Promise vs jQuery Deferred

查看:53
本文介绍了Javascript Promise vs jQuery Deferred的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么优点和优点; Javascript Promises与jQuery Deferred Objects的缺点?

What are the pros & cons of Javascript Promises vs jQuery Deferred Objects?

例如,它们各有什么问题,如果有的话?

For example, what issues do they each have, if any?

一个堆栈溢出成员说:


......真正的承诺是真实的,伪造的承诺是假的。

"...real promises are real, and fake promises are fake."

他的意思是什么?

推荐答案

没有真正的承诺或假承诺。承诺实施遵循当前标准,并且承诺实施不遵循当前标准。承诺的固有属性并不是说它是真实的或假的。它们是实现标准行为的Javascript。

There is no such thing as a "real promise" or a "fake promise". There are promise implementations that follow the current standards and there are promise implementations that do not follow the current standards. There is no inherent attribute of a promise that says it's "real" or "fake". They are bits of Javascript that implement a standard behavior.

严格遵循标准的任何承诺实施应该可以使用,并且应该与其他标准承诺实现互操作。大多数人会认为互操作性和对已接受标准的遵守是一个有用的特性。

Any promise implementation that rigorously follows the standard should be fine to use and should be interoperable with other standard promise implementations. Most will consider interoperability and adherence to accepted standards a useful characteristic.

现在,jQuery承诺不遵循承诺标准,这是有些人不喜欢jQuery承诺的地方。他们显然正在努力使它们成为未来jQuery版本的标准,但它们目前在许多方面偏离了标准。这导致了一个问题,即使用jQuery promises时使用的代码与使用标准promises时的代码不同。所以jQuery promise代码与ES6 promise代码看起来不一样。

Now, jQuery promises do not follow the promise standards which is where some people talk bad of jQuery promises. They are apparently working on making them more standard for a future version of jQuery, but they currently deviate from the standards in many ways. This leads to issues in that you have to code differently when using jQuery promises than when using standards promises. So jQuery promise code does not look the same as ES6 promise code.

jQuery Promises中的问题

请参阅此参考查看当前jQuery承诺的问题列表。两个主要问题是在拒绝承诺中处理错误的问题 执行顺序不一致.then()回调,这可能导致不可预测或不一致的代码执行。所有 .then()处理程序应该是异步执行的(在JS的当前线程解除之后)。 jQuery承诺并不总是这样做,它可能会导致某些类型的代码出现问题。

See this reference for a list of problems with the current jQuery promises. The two main ones are a problem with error handling in rejected promises and an inconsistency in execution order of .then() callbacks which can lead to unpredictable or inconsistent code execution. All .then() handlers are supposed to be executed async (after the current thread of JS unwinds). jQuery promises do not always do this and it can cause problems in some types of code.

浏览器中的本机承诺状态

原生承诺在浏览器中是一个新东西,所以现在还不是你可以依赖这样一个事实:所有可能访问你网站的浏览器都必须内置本机承诺原始承诺内置于浏览器中,从Safari 7.1,Firefox 29,Chrome 32,Edge,Android 4.4.4,IOS 8.4开始,尚未在IE中提供(截至2015年9月)。因此,您仍然需要一个相当新的浏览器才能获得原生承诺支持。因此,有许多优秀的承诺库可以提供polyfill或全面替换,从而使您的代码能够在任何浏览器中使用promises,而不是本机承诺。

Native promises are a somewhat new thing in browsers, so it's not yet the case that you can just rely on the fact that all browsers that might hit your site will necessarily have native promises built in. Native promises are built into browsers starting with Safari 7.1, Firefox 29, Chrome 32, Edge, Android 4.4.4, IOS 8.4 and not available yet in IE (as of Sept, 2015). So, you still need a fairly recent browser in order to have native promise support. As such, there are many excellent promise libraries that can offer either a polyfill or a full-on replacement, thus giving your code the ability to use promises in any browser with really no compromise vs. native promises.

这篇关于Javascript Promise vs jQuery Deferred的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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