0

Why is the error handler not working?

``` new Promise(function(resolve, reject) { setTimeout(() => { throw new Error("Whoops!"); }, 1000); }).catch(alert); ```` Can anyone explain why the error handler is not working here? It works without settimeout. [Please don't paste random links]. Thanks.

7th Aug 2021, 1:12 PM
NBinte
NBinte - avatar
4 Answers
+ 5
NBinte This appears to be relevant. https://stackoverflow.com/questions/48969591/why-promise-can-not-catch-the-error-throw-by-settimeout In short, catch fails to catch the thrown error since the method is executed in a different stack frame. I am not too knowledgeable about how the stack operates, but the thread I linked includes an answer which go into those details. The second answer explains how you need to call 'reject' instead.
7th Aug 2021, 1:33 PM
Hatsy Rei
Hatsy Rei - avatar
7th Aug 2021, 1:34 PM
CarrieForle
CarrieForle - avatar
+ 2
Hatsy Rei Thanks, that helped. :D
7th Aug 2021, 1:52 PM
NBinte
NBinte - avatar
+ 1
🅜︎🅡︎ 🅗︎🅐︎🅢︎🅐︎🅝︎ [B͜͡U͜͡S͜͡Y͜͡ ] Sorry didn't get it. :/ That's not what I'm looking for.
7th Aug 2021, 1:19 PM
NBinte
NBinte - avatar