My code is as such:
document.querySelector('#compose-form').onsubmit = () => {
const success = send_email();
if (success === true) {
setTimeout(() => {load_mailbox('sent'); }, 100);
}
return false;
}
the send_email function:
async function send_email() {
// some code here
const response = await fetch('/emails', {
method: 'POST',
body: JSON.stringify({
recipients: recipients,
subject: subject,
body: body
})
})
// more code here
}
When the .onsubmit is executed, send_email executes and runs smoothly until we reach the fetch call. Running through the debugger, when the whole fetch code block executes, the next step is not to continue through send_email. Instead, the remainder of the .onsubmit event executes without a proper value being assigned to my const success, and only once this is complete does the remainder of send_email run.
My questions are:
- What is causing this behavior? I would expect the entirety of send_email to run before continuing with the .onsubmit handler.
- What would be the proper way to accomplish what I am going for here, executing the whole of send_email including the API fetch call before completing the later code in the .onsubmit event?
All help is appreciated, thanks!