Javascript ES5 与 ES6 承诺
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38424517/
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
ES5 vs ES6 Promises
提问by Param Singh
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.
我想知道 JS 承诺是否是 es5 的一部分?如果是这样,为什么它有时在旧浏览器中不起作用,我们必须为此添加一个 polyfill。另外,在这种情况下应该添加哪个 polyfill,是 es5 还是 es6?我对此有点困惑。
回答by jfriend00
ES5 did not have promises. Libraries like jQuery or Angular had their own custom and non-standard promise implementations.
ES5 没有承诺。像 jQuery 或 Angular 这样的库有自己的自定义和非标准 promise 实现。
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.
与 ES5 一起使用的流行 Promise 实现是 Bluebird(与 ES6 标准兼容)和 Q(最初与 ES6 标准不兼容——尽管似乎正在朝着这个方向发展)和 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.
实际的 polyfill 也不是,因为如果存在原生承诺,它们不会让步,因为它们添加了额外的功能。
There are some pure polyfills for ES6 promises. Here's one that is a subset of RSVP: https://github.com/stefanpenner/es6-promise.
ES6 承诺有一些纯 polyfill。这是 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?
正如您在此处阅读的那样,除了使用 polyfill 之外,还有理由使用:现在我们有了 ES6 承诺,是否还有理由使用 Q 或 BlueBird 等承诺库?
Asking for a recommendation of a specific polyfill library is considered off-topic here on Stack overflow so I won't comment on that.
在 Stack Overflow 上要求推荐一个特定的 polyfill 库被认为是题外话,所以我不会对此发表评论。