Javascript 尝试在删除项目后将总计重新打印到DOM
所以我尝试使用Promise.all方法等待所有操作完成(从firebase中删除项目,然后在其他Promise中重新计算总计,并在屏幕上重新呈现总计)。但每次我这么做,它都比我落后一步。删除的第一个项目根本不会调整总计,我删除的第二个项目会调整删除的第一个项目的值Javascript 尝试在删除项目后将总计重新打印到DOM,javascript,jquery,firebase,promise,es6-promise,Javascript,Jquery,Firebase,Promise,Es6 Promise,所以我尝试使用Promise.all方法等待所有操作完成(从firebase中删除项目,然后在其他Promise中重新计算总计,并在屏幕上重新呈现总计)。但每次我这么做,它都比我落后一步。删除的第一个项目根本不会调整总计,我删除的第二个项目会调整删除的第一个项目的值 $('body').on('click', '.delete-event-food', (e) => { e.stopImmediatePropagation(); const firebaseKey =
$('body').on('click', '.delete-event-food', (e) => {
e.stopImmediatePropagation();
const firebaseKey = e.currentTarget.id;
$(`.line-item#${firebaseKey}`).remove();
Promise.all([
eventFood.deleteFoodOfEvent(firebaseKey),
eventShows.showsTotalPrices(eventFirebaseKey),
eventSouvenirs.souvenirsTotalPrices(eventFirebaseKey),
eventStaff.staffTotalPrices(eventFirebaseKey),
eventFood.foodTotalPrices(eventFirebaseKey),
]).then((values) => {
$('#foodTotalCost').html(`${values[4]}`);
const eventTotal = values.reduce((a, b) => a + b);
$('#eventTotal').html(
`<h2 id="eventTotalBanner"> The Total Cost is ${eventTotal}</h2>`
);
});
});
这可能与突然出现的神秘的
eventFirebaseKey
变量有关吗?
const foodTotalPrices = (eventFirebaseKey) => new Promise((resolve, reject) => {
let foodTotal = 0;
getEventFood(eventFirebaseKey)
.then((foodArray) => Promise.all(foodArray.map((food) => foodData.getSingleFoodItem(food.foodUid))))
.then((foodObjects) => foodObjects.forEach((food) => {
foodTotal += parseInt(food.price, 10);
}))
.then(() => resolve(foodTotal))
.catch((error) => reject(error));
});