The following line of code is able to catch the error (as it is sync)
new Promise(function(resolve, reject) {
throw new Error("Whoops!");
}).catch(alert);
But when I modify my code like below
new Promise(function(resolve, reject) {
setTimeout(() => {
throw new Error("Whoops!");
}, 1000);
}).catch(alert);
It is not able to catch the error. I have a use case where I want to catch this error. How can I achieve it?
Following the link "https://bytearcher.com/articles/why-asynchronous-exceptions-are-uncatchable/" I am able to understand why is it happening. Just want to know is there still any solution to catch such an error.
Kindly note, By the use of setTimeout, I am pointing the use of async call which can give some response or can give error as in my case when I supply incorrect URL in a fetch statement.
fetch('api.github.com/users1') //'api.github.com/user'is correct url
.then(res => res.json())
.then(data => console.log(data))
.catch(alert);