internet-explorer - .then 是否受 Internet Explorer 支持?
问题描述
我读过 IE 不支持承诺,但是我有带有 .then 的遗留代码,它似乎在 IE 上运行良好。
我的理解是 .then = promises,这怎么可能?
谢谢
解决方案
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 方法。
推荐阅读
- postgresql - Docker 从 yml postgres 数据库 url 错误组成
- docker - Docker Compose:将输出记录到文件和标准输出
- sql - 按问题分组 GreenPlum
- python-sphinx - 将代码类型传递给 Sphinx `.. include::` 指令
- rust - 如何避免`#[global_allocator]`冲突?
- amazon-web-services - 尚未为演示项目创建 EKS 入口的公共地址
- ios - 将 Swift 应用程序连接到 AWS 数据库
- influxdb-2 - InfluxDB Flux查询每组最后一条记录
- neo4j - neo4j 按可选属性按降序排列,空值位于底部
- python - 尝试在 Python 中对谷歌字体进行 base64 编码时出现问题