我是 JavaScript 上 Promises 的新手,所以我希望有人能帮助我解决这个问题。
问题:Promise 无法在 IE11 上执行,但在 Chrome 和 FireFox 上运行良好
使用的框架:我尝试使用 es6-promise.d.ts 和 bluebird.d.ts 结果相同。
代码:
static executeSomething(): Promise<any>
{
console.log("inside executeSomething");
var test= new Promise((resolve, reject)=>
{
console.log("inside Promise");
}).catch(function(error){console.log("error")});
console.log("after promise");
return test;
}
结果: 在 chrome 和 Firefox 上我可以看到所有日志,但在 IE11 上我只看到“Inside executeSomething”,这意味着问题出在创建 promise 时。
我以为是因为 IE11 不支持 es6 但我使用 bluebird 得到了相同的结果,我希望有人能为我的问题带来一些启发。
最佳答案
您需要在页面中包含一个 promise polyfill 才能使 IE11 正常工作。
你使用 es-promise 的直觉是正确的,但你还需要在你的 html 中包含 .js
文件
<script src="path/to/es6-promise.js"></script>
.d.ts
文件将为 TypeScript 编译器提供它的定义,但不会影响运行时。您仍然需要在您的 html 中包含 polyfill,以便它在浏览器中实际运行。
在使用 TypeScript 或任何编译语言时要记住的最重要的事情是编译时间和运行时间之间的区别。
.d.ts
、.ts
、.tsx
等都是编译时的文件。这意味着这些不是实际执行的文件,而是生成运行时代码的文件。
.js
文件是运行时 文件。这些是由浏览器运行的文件。
.d.ts
文件不包含代码,而是包含代码签名的定义,因此应该始终附有将在浏览器中运行的相应 .js
文件。
关于javascript - Promises 不适用于 IE11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35236782/