如何在 Internet Explorer 11 中支持承诺 [英] How to support promises in Internet Explorer 11

查看:31
本文介绍了如何在 Internet Explorer 11 中支持承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的代码,可以在除 Internet Explorer 11 之外的所有浏览器上完美运行.如何使其在所有浏览器上都能正常运行?

代码笔

提前致谢.

'use strict';让承诺 = 新承诺((解决,拒绝)=> {setTimeout(() => {解决(结果");}, 1000);});承诺.然后(结果 =>{alert("已完成:" + 结果);},错误 =>{alert("拒绝:" + 错误);});

解决方案

如果你想让这种类型的代码在 IE11(它根本不支持很多 ES6)中运行,那么你需要得到一个 3rd party promise library(例如 Bluebird),包含该库并更改您的编码以使用 ES5 编码结构(没有箭头函数,没有 let 等...)这样您就可以在旧浏览器支持的范围内生活.

或者,您可以使用转译器(如 Babel)将您的 ES6 代码转换为可在旧浏览器中使用的 ES5 代码.>

这是您使用 Bluebird 承诺库以 ES5 语法编写的代码版本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js"></script><脚本>'使用严格';var promise = new Promise(function(resolve) {设置超时(功能(){解决(结果");}, 1000);});promise.then(函数(结果){alert("已完成:" + 结果);}, 函数(错误){alert("拒绝:" + 错误);});

I have a simple code that runs perfectly on every browser except for the Internet Explorer 11. How can I make it work on all browsers?

Codepen

Thanks in advance.

'use strict';

let promise = new Promise((resolve, reject) => {

  setTimeout(() => {
    resolve("result");
  }, 1000);
});

promise
  .then(
    result => {
      alert("Fulfilled: " + result);
    },
    error => {
      alert("Rejected: " + error);
    }
  );

解决方案

If you want this type of code to run in IE11 (which does not support much of ES6 at all), then you need to get a 3rd party promise library (like Bluebird), include that library and change your coding to use ES5 coding structures (no arrow functions, no let, etc...) so you can live within the limits of what older browsers support.

Or, you can use a transpiler (like Babel) to convert your ES6 code to ES5 code that will work in older browsers.

Here's a version of your code written in ES5 syntax with the Bluebird promise library:

<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js"></script>

<script>

'use strict';

var promise = new Promise(function(resolve) {
    setTimeout(function() {
        resolve("result");
    }, 1000);
});

promise.then(function(result) {
    alert("Fulfilled: " + result);
}, function(error) {
    alert("Rejected: " + error);
});

</script>

这篇关于如何在 Internet Explorer 11 中支持承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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