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.
5 Respostas
+ 6
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.
+ 3
Try using throw and catch inside the method
https://stackoverflow.com/questions/41431605/handle-error-from-settimeout
+ 2
Hatsy Rei
Thanks, that helped. :D
+ 1
🅜︎🅡︎ 🅗︎🅐︎🅢︎🅐︎🅝︎ [B͜͡U͜͡S͜͡Y͜͡ ]
Sorry didn't get it. :/ That's not what I'm looking for.