0

I have a function that makes calls to multiple asynchronous functions.

$("#signUpForm").validator().on("submit", function(event) {
if (event.isDefaultPrevented()) {
    // handle the invalid form...
    sformError();
    ssubmitMSG(false, "Please fill all fields!");
} else {
    // everything looks good!
    event.preventDefault();
    
    //create use when form is submit
    ssubmitForm();
    
    //send email verification
    sendEmailVerification();

    //sign out the user
    signOut();
    
  
}
});

the functions ssubmitForm() , sendEmailVerification() and SignOut() are all declared asynchronous and all have await statements in them.

I am getting weird behavior that seems to signal to me that these three functions are not running in the order they are written in the code. I thought that with asynchronous code all other code pauses execution and waits for the await statement to finish. But from my logs, I neah function it doesn't seem so. Is this behavior only within the scope of the asynchronous function itself? What is the execution order of the three functions? Can sendEmailVerification() be called before ssubmitForm() is done execution?

Also, I'm not sure if I'm using the correct verbiage/vocabulary to describe my problem. This has been a big problem for me as I am not easily able to search for my issues if I don't know the vocab. Please feel free to correct me on any misuse of terminology.

5
  • 1
    Making asynchronous programming easier with async and await Commented Aug 11, 2020 at 6:53
  • Because you are not awaiting them: await ssubmitForm() etc. Commented Aug 11, 2020 at 6:55
  • You think it's weird that asynchronous code doesn't run synchronous? :p That is not weird, that is expected. Commented Aug 11, 2020 at 6:55
  • @Wimanicesir I am using await to try to add some synchronicity to it. Commented Aug 11, 2020 at 6:56
  • Checkout Promise.all or use then after each async method. ssubmitForm().then(() => sendEmailVerification() ) ; Commented Aug 11, 2020 at 6:57

1 Answer 1

1

They are started in the order that they are called, but because they are async, they might not run in the order they are called. They return immediately and return an unresolved Promise, which will eventually resolve (or reject) asynchronously when the called function completes.

You need to mark your function async and await each of the calls in turn:

$("#signUpForm").validator().on("submit", async function(event) {
if (event.isDefaultPrevented()) {
    // handle the invalid form...
    sformError();
    ssubmitMSG(false, "Please fill all fields!");
} else {
    // everything looks good!
    event.preventDefault();
    
    //create use when form is submit
    await ssubmitForm();
    
    //send email verification
    await sendEmailVerification();

    //sign out the user
    await signOut();
    
  
}
});

To better understand async/await, I recommend learning about promises first. They are the underlying model that makes everything tick; async/await is just syntactic sugar on top.

Sign up to request clarification or add additional context in comments.

1 Comment

I see. I wasn't sure where to put the async. I tried putting it in front of the listener. I guess I got confused trying to use boilerplate code. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.