How can I call an async function from an event handler and surface its errors?
Quick answer
Make the handler async, handle the awaited work with try/catch, and use finally to restore the busy state whether the work succeeds or fails. Put event-specific decisions—such as whether to prevent a form’s normal submission—at the start of the handler, separately from the request. Show progress and a useful error in the interface; don’t leave a rejected operation with no visible outcome. [S1] [S2]
The question, the details, the next step.
A practical checklist
Decide whether the handler should prevent the event’s ordinary behavior; keep that decision separate from the request.
Show a busy state before awaiting the operation.
Handle the awaited operation’s rejection with
catchortry/catch.Give the user a clear success or failure status without claiming more than the operation confirms.
Restore the control in
finallyso cleanup occurs after success or failure.
A few more useful answers
Should I use `.catch()` or `try`/`catch`?
What should the error message say?
Use a brief message that tells the user the operation did not complete and what reasonable next step to take. Avoid reporting success or claiming the data’s state unless the operation confirms it.
Sources
References used for this guide.
- [S1] JavaScript Promise reference: handling rejected promises learn.microsoft.com · Checked
- [S2] JavaScript try...catch: exception handling and cleanup learn.microsoft.com · Checked
