如何使承诺在IE11中工作 [英] How to make promises work in IE11

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

问题描述

我有一个简单的代码可以在除Internet Explorer 11之外的每个浏览器上运行。我如何才能在所有浏览器上运行?

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

提前致谢。

'use strict';

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

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

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


推荐答案

如果您希望此类代码在IE11中运行(它根本不支持ES6的大部分),那么你需要获得第三方承诺库(如 Bluebird ),包括该库并更改您的编码以使用ES5编码结构(没有箭头功能,没有等等)所以你可以生活在什么范围内旧浏览器支持。

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.

或者,您可以使用转发器(如 Babel )来将您的ES6代码转换为适用于旧版浏览器的ES5代码。

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

以下是使用Bluebird promise库以ES5语法编写的代码版本:

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>

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

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