How to send multiple API request? so I have this code below
const productID= [12, 43, 65, 87, 45, 76, 87];
productID.map(id => {
API.removeItem(id, (err, response) => {
if (response.status === 'success') {
console.log('remove a product');
}else {
console.log(err);
}
})
})
The problem is that it doesn't wait for first API request to finish and get the response back, and only removing one item. So basically I wanted this to become synchronous and that's why I use async await to resolve my problem.
async function removeProducts() {
const productID = [12, 43, 65, 87, 45, 76, 87];
const result = await Promise.all(productID.map(id => {
API.removeItem(id, (err, response) => {
if (response.status === 'success') {
console.log('remove a product');
}else {
console.log(err);
}
});
}));
}
The result of this is almost the same as the first code snippet, but it this time it was able to remove 2 product items. I want that the next request is made only if the previous request is finished. How can I do that?