How to make promises work in IE11

后端 未结 2 791
你的背包
你的背包 2020-11-27 03:24

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 adva

相关标签:
2条回答
  • 2020-11-27 04:05

    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>
    
    0 讨论(0)
  • 2020-11-27 04:07

    You could try using a Polyfill. The following Polyfill was published in 2019 and did the trick for me. It assigns the Promise function to the window object.

    used like: window.Promise https://www.npmjs.com/package/promise-polyfill

    If you want more information on Polyfills check out the following MDN web doc https://developer.mozilla.org/en-US/docs/Glossary/Polyfill

    0 讨论(0)
提交回复
热议问题