You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What specific section or headline is this issue about?
No response
What information was incorrect, unhelpful, or incomplete?
The article on the error event states that the event is fired when script errors occur, and the article's code example demonstrates this using a script error. However, the article makes no mention of the fact that this only applies to errors in non-async JavaScript functions.
When an error halts execution of an async JavaScript function, this is treated as a rejection of the promise tied to the function's execution, with the error object being the rejection reason. If the async function has no handlers attached, then this triggers an unhandledrejection event. To catch errors that occur in async execution, then, you'd need something like this:
window.addEventListener("unhandledrejection",function(e){if(!(einstanceofError))return;// do stuff here});
What did you expect to see?
Up-front clarification of the fact that error is only for synchronous code and unhandledrejection is needed for errors in async code, preferably in one of those little red-colored boxes MDN uses for warnings and caveats.
Josh-Cena
added
accepting PR
Feel free to open a PR to resolve this issue
and removed
needs triage
Triage needed by staff and/or partners. Automatically applied when an issue is opened.
labels
Feb 20, 2025
MDN URL
https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event
What specific section or headline is this issue about?
No response
What information was incorrect, unhelpful, or incomplete?
The article on the
error
event states that the event is fired when script errors occur, and the article's code example demonstrates this using a script error. However, the article makes no mention of the fact that this only applies to errors in non-async
JavaScript functions.When an error halts execution of an
async
JavaScript function, this is treated as a rejection of the promise tied to the function's execution, with the error object being the rejection reason. If the async function has no handlers attached, then this triggers anunhandledrejection
event. To catch errors that occur in async execution, then, you'd need something like this:What did you expect to see?
error
is only for synchronous code andunhandledrejection
is needed for errors inasync
code, preferably in one of those little red-colored boxes MDN uses for warnings and caveats.Do you have any supporting links, references, or citations?
No response
Do you have anything more you want to share?
GitHub doesn't allow uploading HTML files, so if you want a quick demonstration, here you go:
MDN metadata
Page report details
en-us/web/api/window/error_event
The text was updated successfully, but these errors were encountered: