0

I have below function inside an async function.

await saveStock(stock).then((res) => {
 addDrugToStock(stockDrug)
  addSupplierToStock(stockSupplier)

});

When I run this it always saveStock function gives appropriate results. but the other two functions are not executing properly. Sometimes addDrugToStock is working. In other times adSupplierToStock is working. How can I run both addDrugToStock and addSupplierToStock at a same time.

2
  • 1
    How can I **run** both... at a same time. You cannot run but you can get the values at the same time Commented Apr 17, 2020 at 19:21
  • 1
    Use Promise.all for parallel function call stackoverflow.com/questions/35612428/… Commented Apr 17, 2020 at 19:32

2 Answers 2

1

If you want to execute both Promise objects and wait for both of them, then you need to use Promise.all: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

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

Comments

1

When using async/await operators you don't have to implement the .then() or .catch() handlers, just write code the synchronous way. Otherwise, you can use Promise.all to handle multiple independent Promises.

try {
   // Equal to .then() in regular Promises

   const response = await saveStock(stock);

   addDrugToStock(stockDrug);
   addSupplierToStock(stockSupplier);
} catch (e) {
   // Equal to .catch() in regular Promises
}

Comments

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.