I am creating a simple react pharmacy application in which I'm supposed to change remove all the medicines from a certain group and then delete the group.
I have the two functions created like this.
1. changeMedicineGroupFunction
const changeMedicineGroup = (medicineId, groupIdToChangeTo) => {
fetch(
`${process.env.REACT_APP_API_ROOT_URL}/changeMedicineGroup/${medicineId}/${groupIdToChangeTo}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
}
)
.then((res) => res.text())
.then((response) => console.log(response));
};
2.deleteGroupFunction.
const deleteGroup = () => {
fetch(`${process.env.REACT_APP_API_ROOT_URL}/deletegroup/${data.groupId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
})
.then((res) => res.text())
.then((response) => console.log(response));
};
then a final function is used to invoke the two functions above as follows
const removeMedicinesFromGroup = async () => {
let unSetGroupId = 24;
groupMedicines.forEach((medicine) =>
changeMedicineGroup(medicine.medicineId, unSetGroupId)
);
deleteGroup();
};
QUESTION How do I make my removeMedicinesFromGroup()
function asynchronous in such a way that the deleteGroup()
function is only invoked when the code above it is complete i.e the changing medicine Group
logic. I want to use async-await. This is crucial for my application because if I delete a group while it still has data I have like whole foreign key constraint errors I'm trying to avoid.
Please Help.