ES5与ES6的承诺 [英] ES5 vs ES6 Promises

查看:88
本文介绍了ES5与ES6的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道JS承诺是否属于es5的一部分?如果是这样,为什么它有时在旧版浏览器中不起作用,我们必须添加一个polyfill。另外,在这种情况下应该添加哪种polyfill,es5 one或es6?我对此有一点困惑。

解决方案

ES5没有承诺。像jQuery或Angular这样的库有自己的自定义和非标准的promise实现。



与ES5一起使用的流行Promise实现是Bluebird(它与ES6标准兼容)和Q(最初并不与ES6标准兼容 - 虽然似乎正朝着这个方向发展)和RSVP。



实际的polyfills也不是因为他们添加了额外的功能,所以如果存在原生承诺,它们就不会被挡住。



ES6承诺有一些纯的polyfill。这是RSVP的一个子集: https://github.com/stefanpenner/es6-promise



您可以阅读以下内容,有理由使用多种填充剂:是否还有理由使用Q或BlueBird这样的承诺库ES6承诺?



在Stack溢出时,要求推荐特定的polyfill库被认为是偏离主题的,所以我不会对此发表评论。 / p>

I wanna know whether JS promises were a part of es5? If so, why it doesn't work sometimes in older browsers and we have to add a polyfill for the same. Also, which polyfill should be added in that case, an es5 one or es6? I have a little confusion regarding the same.

解决方案

ES5 did not have promises. Libraries like jQuery or Angular had their own custom and non-standard promise implementations.

Popular Promise implementations for use with ES5 are Bluebird (which is compatible with the ES6 standard) and Q (which was not originally compatible with the ES6 standard- though seems to be moving that direction) and RSVP.

Neither are actual polyfills in that they don't get out of the way if native promises are present because they add additional features.

There are some pure polyfills for ES6 promises. Here's one that is a subset of RSVP: https://github.com/stefanpenner/es6-promise.

There are reasons to use more than a polyfill as you can read here: Are there still reasons to use promise libraries like Q or BlueBird now that we have ES6 promises?

Asking for a recommendation of a specific polyfill library is considered off-topic here on Stack overflow so I won't comment on that.

这篇关于ES5与ES6的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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