zone.js promises returning Unhandled Promise Rejection error even with catch blocks
See original GitHub issueI am working on an angular environment with zone.js so all the native es6 promises have been over-written by zone-aware promises. I have 2 functions called save() and onSaveCb()
save() calls a function called validate() which gets the error-codes of any of the saved objects already present in the file so it is done in sync.
if hasError() returns true we return Promise.reject() from save() otherwise we send a save request to the backend.
Now, onSaveCb() function is an async/await function which has a try-catch block around save().
Problem:
When Promise is rejected from save() function, I see console log error:
zone.js:682 Unhandled Promise rejection: ; Zone: ; Task: onClick Event ; Value: Error:
I tried a bunch of things with wrapping the function save in a promise and then rejecting it, but i still see the same error
function save() {
let errorCode = this.validate(); // This is a sync call
if(errorCode !== 0) { return Promise.reject(errorCode);}
return https(url).then(function() {return true});
}
function async onSaveCb() {
try{
let response = await this.save();
alertUI(response);
} catch (response) {
alertUI(response);
}
}
I am not sure why I am getting this message when I have clearly written error handling in the catch block. Any help here would be helpful
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
@Kanti, sure, I have fixed this one, but it break some cases inside
Google
, so I am working on to provide anoption flag
to enable/disable thisuncaught promise error
feature ofzone.js
.Is there any update on this issue? I’m seeing warnings about promises I actually catch as well (using
promise.then(x => { }, err => { })
). I don’t want to totally disable Zone’s uncaught promise detection feature – it would be great if it worked! – but I’d like it to stop spuriously telling me I didn’t catch something that is in fact caught.