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

Do these two async functions run in parallel?

Let’s say we have two async functions like so:

const getUrl = async () => ...logic

const getName = async () => ...logic

and then I want to define an object which the values of the keys are these functions such as:

const defineObject = async() => {

   const obj = {
      url: await getUrl()
      name: await getName()
   }
}

Is this going to behave as generally JS does where it waits for the first async function to run getUrl() and then runs the second async function getName(), or, does it run them in parallel.

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 :

No, they won’t run in parallel. Each use of await turns into a getXXX().then(), with the code after it put into the .then() callback function.

If you want to do that, you can use Promise.all().

const [url, name] = await Promise.all([getUrl(), getName()]);
const obj = {url, name};
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