Cét*_*tia 30 internet-explorer internet-explorer-8 promise angularjs angular-promise
在尝试捕获承诺拒绝时,我在IE8上遇到了一个奇怪的错误(基本ngResource调用返回的承诺):
此代码使用.then(success, fail)语法:
promise.then(function(response) {
// success
},
function(response) {
// error
});
Run Code Online (Sandbox Code Playgroud)
但这个失败的.then(success).catch(fail)语法:
promise.then(function(response) {
// success
})
.catch(function(response) {
// error
});
Run Code Online (Sandbox Code Playgroud)
并且指向该.catch()行的IE错误是:
预期的标识符
难道我做错了什么 ?有人重现了吗?或者由于受限制的关键字,它是一个常见的IE8?
谢谢
Ben*_*aum 49
您需要使用括号表示法:
promise.then(function(response) {
// success
})
["catch"](function(response) {
// error
});
Run Code Online (Sandbox Code Playgroud)
这是因为IE8实现了ECMAScript 3,它不允许以点表示法表示裸关键字.现代浏览器实现允许它的ECMAScript 5.
很多库.catch使用另一个关键字别名.然而,建立Angular承诺的方式扩展$q承诺并不简单.所以["catch"]必须这样做.请注意,这也是如此finally.
是的,IE8认为它是一个关键字.你可以通过以下两种方式解决这个问题:
promise.then(function() { })['catch'](function() { });promise.then(function() { /* success handler */ })).then(null, function() { /* error handler */ });then语句中,如果这样的话是合适的:promise.then(function() { /* success handler here */ }, function() { /* error handler here */ });catch 是#2的简写.
| 归档时间: |
|
| 查看次数: |
7925 次 |
| 最近记录: |