I have searched for answers but none seem to directly address my specific question.
My current usage involves the pattern shown below:
class A
{
getLocation()
{
return Promise.reject(2222222)
}
async a()
{
try
{
var loc = await this.getLocation();
alert(loc)
}
catch (e)
{
alert("catch 2")
}
}
}
new A().a();
- Result : "catch 2"
Even when I introduce an error in the getLocation
function :
getLocation()
{
throw Error("ffffff")
}
- I still get the same result - which is expected.
But here lies the issue:
The behavior of errors thrown asynchronously-non-promised
is different:
So this code won't be caught at all:
getLocation() //bad code from a third party code , third party code
{
return new Promise((v, x) => setTimeout(() =>
{
throw Error("ffffff")
}, 100))
}
Question:
Considering that I want to capture these errors, is there a better approach?
I could simply do:
window.onerror = function () { alert(4)}
However, this would not align with the flow of .catch(...)
or catch(){}
, and I wouldn't be able to handle actions specifically related to the error-causing event.
Full disclosure:
This scenario is purely for educational purposes.