I'm experiencing a weird bug on IE8 while trying to catch a promise reject (promise returned by a basic ngResource
call) :
This code work with .then(success, fail)
syntax :
promise.then(function(response) {
// success
},
function(response) {
// error
});
but this one fails with .then(success).catch(fail)
syntax :
promise.then(function(response) {
// success
})
.catch(function(response) {
// error
});
and the IE error pointing to the .catch()
line is :
Expected identifier
Am I doing something wrong ? someone reproduce it ? or is it a common IE8 due to restricted keyword ?
Thanks
You need to use bracket notation:
promise.then(function(response) {
// success
})
["catch"](function(response) {
// error
});
This is because IE8 implements ECMAScript 3 that does not allow bare keywords in dot notation. Modern browsers implement ECMAScript 5 that allows it.
A lot of libraries alias .catch
with another keyword. However, the way Angular promises are built it is not simple to extend $q
promises. So ["catch"]
would have to do. Note this is also true for finally
.
Yes, IE8 thinks it's a keyword. You can get around this a couple of ways:
promise.then(function() { })['catch'](function() { });
promise.then(function() { /* success handler */ })).then(null, function() { /* error handler */ });
then
statement, if such a thing is appropriate: promise.then(function() { /* success handler here */ }, function() { /* error handler here */ });
catch
is shorthand for #2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With