Javascript 有没有办法在ie9+中实现promise

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27835687/
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-08-23 00:44:12  来源:igfitidea点击:

is there a way to implement promises in ie9+

javascriptjqueryangularjsinternet-explorerpromise

提问by user1019872

I m trying to implement native promiseon IE9+and it seems that internet explorer does not Recognize the "Promise" class/object. I searched in http://caniuse.com/#search=Promiseand saw that IEdon't have Promise object. But this is strange because jQueryand Angularworks with promises and working in IE9+.Maybe they are implementing their own Promise??** Can i implement my own promise ?**

我正在尝试在IE9+上实现本机承诺,但 Internet Explorer 似乎无法识别“承诺”类/对象。我在http://caniuse.com/#search=Promise 中搜索,发现IE没有 Promise 对象。但这很奇怪,因为jQueryAngular可以使用 promises 并且可以在 IE9+ 中使用。也许他们正在实现自己的承诺?** 我可以实现我自己的承诺吗?**

回答by Benjamin Gruenbaum

What's "native"

什么是“原生”

Being native means that it is written inside the browser's codebase and not in user-level code. In order to implement promises natively in IE9 you'd have to obtain a copy of the IE9 source code from Microsoft, code those in, compile and distribute it. While that's doable that's not a very viable option.

原生意味着它是在浏览器的代码库中编写的,而不是在用户级代码中。为了在 IE9 中本地实现 promise,您必须从 Microsoft 获得一份 IE9 源代码的副本,编写代码,编译并分发它。虽然这是可行的,但这不是一个非常可行的选择。

Using promises today

今天使用 Promise

On the other hand - there are several promise libraries that exist in userlevel. It's perfectly possible to implement your own promise implementation - here is a great blog post on how.

另一方面 - 在userlevel中存在几个承诺库。完全有可能实现您自己的 promise 实现 -这是一篇关于如何.

You can freely include a library like bluebird(bluebird) or Q that fulfill this API and will continue working well with ES6 promises in the future. It is worth mentioning that Bluebird for instance runs on IE6+ so there should be no issues in your case.

你可以自由地包含一个像bluebird( bluebird) 或 Q 这样的库来实现这个 API,并且在未来将继续与 ES6 承诺一起工作。值得一提的是,例如 Bluebird 在 IE6+ 上运行,因此您的情况应该没有问题。

For example here is Angular's implementationand here is jQuery's implementation- Angular's is more readable in my opinion.

例如,这里是 Angular 的实现,这里是jQuery 的实现——在我看来,Angular 的可读性更强。

The standard

标准

Also note that contrary to the coment left above promises arestandardized by the Promises/A+ specification with libraries like Angular conform to and libraries like jQuery are working on conforming to and likely will in the next version. Native promises also conform to the standard and superset it.

另请注意,与上面留下的评论相反,Promises/A+ 规范对 Promises/A+ 规范进行了标准化,其中 Angular 之类的库符合,而 jQuery 之类的库正在努力符合并且可能会在下一个版本中符合。本机承诺也符合标准并对其进行超集。

回答by bogdan.mustiata

Here's a promise implementationI wrote myself in typescript, and is tested to fully work from IE8 upwards. Yes. it's fully Promises/A+ conformant (and tested).

这是我自己在打字稿中编写的承诺实现,经过测试可以从 IE8 开始完全工作。是的。它完全符合 Promises/A+ 标准(并经过测试)。

Since it's written in typescript, the plan is to also get the definition up for tsd in the following days.

由于它是用打字稿编写的,因此计划在接下来的几天内也为 tsd 提供定义。