首页 > 解决方案 > .then 是否受 Internet Explorer 支持?

问题描述

我读过 IE 不支持承诺,但是我有带有 .then 的遗留代码,它似乎在 IE 上运行良好。

我的理解是 .then = promises,这怎么可能?

谢谢

标签: internet-explorerdeferred

解决方案


Promise.prototype.then() 方法返回一个 Promise。此方法仍然不支持 IE 浏览器。您可以检查浏览器兼容性

要在 IE 11 浏览器中使用 Promise 和 Then 方法,您可以使用 3rd 方 Promise 库(如 Bluebird)或Babel转译器将 ES6 代码转换为 ES5 代码。请参考以下示例代码:

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

<script>
    (function (global, factory) {
        if (typeof define === "function" && define.amd) {
            define([], factory);
        } else if (typeof exports !== "undefined") {
            factory();
        } else {
            var mod = {
                exports: {}
            };
            factory();
            global.repl = mod.exports;
        }
    })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () {
        "use strict";

        var myFirstPromise = new Promise(function (resolve, reject) {
            // We call resolve(...) when what we were doing asynchronously was successful, and reject(...) when it failed.
            // In this example, we use setTimeout(...) to simulate async code. 
            // In reality, you will probably be using something like XHR or an HTML5 API.
            setTimeout(function () {
                resolve("Success!"); // Yay! Everything went well!
            }, 250);
        });
        myFirstPromise.then(function (successMessage) {
            // successMessage is whatever we passed in the resolve(...) function above.
            // It doesn't have to be a string, but if it is only a succeed message, it probably will be.
            console.log("Yay! " + successMessage);
        });
    });
</script>

IE浏览器的输出:

在此处输入图像描述

编辑:

deferred.then()方法支持 IE 浏览器。也许您正在使用 deferred.then 方法。


推荐阅读