Reputation: 239
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.
Upvotes: 0
Views: 42
Reputation:
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
}
Upvotes: 1
Reputation: 353
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
Upvotes: 1