Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
Wait for a function to finish, before the next one executes

I have a react app. In it when a user summits the form, I want to open a modal, and store the users response in state. Based on this reponse I want to preform some business logic, then post the form.

This is my onsubmit method

      async function onSubmit(submission: IPayload) {
        await handleModal();
        await submit(submission);
      }
    
      async function submit(submission: ICreateAdPayload) {
          await api.create({ this is where all the props go})
      }

     // Heres I set the state of the modal to open, which then chanes the open prop on another component for the modal to open.

      async function handleModal(submission: any) {
        setShowModal(true);
    
        // handle logic

        setShowModal(false);
  }

However my submit function is being carried out as soon as the modal opens. Any idea?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To be able to await handleModal(...) then that method must return a Promise which you resolve when it's work has finished - assuming it itself does not need to await anything, it does NOT need to be marked async

function handleModal(submission: any) {
    setShowModal(true);

    return new Promise(resolve => {
        // handle logic 

        setShowModal(false);

        resolve();
    });
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!