Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I call my dialog before multiple async/await starts and close after those calls are completed?

I want call my dialog box before the sequence of multiple async/await calls. This is my code:

document.getElementById("dialog").showModal();

if(condition 1 is true){
    if(condition 2 is true){
        (async()=>{
             await f1();
        })();
    }
}

if(condition 3 is true){
    if(condition 4 is true){
        (async()=>{
             await f2();
        })();
    }
}

if(condition 5 is true){
    if(condition 6 is true){
        (async()=>{
             await f3();
        })();
    }
}

document.getElementById("dialog").close();

Upon executing the code, the dialog opens and closes instantly before the async/await calls even complete. How do I close the dialog only when all the server calls are completed?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The only code that will wait for the promise to resolve is other code in the same async function, after the await. Code outside the async function will not wait. So your tiny async functions do next to nothing, because there is no code after their awaits.

You need to write this code as one large async function.

const someFunction = async () => {
  document.getElementById("dialog").showModal();

  if(condition 1 is true){
    if(condition 2 is true){
      await f1();
    }
  }

  if(condition 3 is true){
    if(condition 4 is true){
      await f2();
    }
  }

  if(condition 5 is true){
    if(condition 6 is true){
      await f3();
    }
  }

  document.getElementById("dialog").close();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading