I'm having a trouble wrapping my head around this problem: suppose I have a form
where I want to handle onSubmit
using async callback through event listener and I want to prevent the default behavior. I don't understand why this works:
form.addEventListener('submit', async(event) => {
// do stuff
event.preventDefault();
await asyncFetching(); // first await
// do more stuff
}
And this doesn't:
form.addEventListener('submit', async(event) => {
// do stuff
await asyncFetching(); // first await
event.preventDefault();
// do more stuff
}
I've come to understand from event.preventDefault in async functions that event will have happened by the time await
unblocks. But I don't get it. This is what I expect:
- Event is triggered once we click the button
- Event handler fires up
- Event handler finishes execution
- Event has happened
What am I missing here?