beforeTabSwitch: async (tab) => {
let flag = false;
if (tab === 'PAYMENT') {
if (this.isManualValidated) {
flag = true;
this.savePayment().then((response) => {
this.placeOrder();
});
}
}
return flag;
}
savePayment: async function () {
this.$http.post(this.savePaymentRoute)
.then(response => {
await this.getOrderSummary();
})
.catch(error => {
});
},
placeOrder: async function () {
this.$http.post(this.saveOrderRoute)
.then(response => {
})
.catch(error => {
console.log('placeOrder | ' error);
})
},
當點擊下單按鈕beforeTabSwitch () 驗證資料然后呼叫savePayment () 。當 savePayment 請求完成后呼叫getOrderSummary () 再呼叫 placeOrder () 請求。
按順序呼叫:savePayment() > getOrderSummary() > placeOrder()
但問題是在完成后立即執行 savePayment() placeOrder() 執行開始然后 getOrderSummary() 執行這是錯誤的。
我已經嘗試過承諾,回呼但同樣的問題。

uj5u.com熱心網友回復:
您需要開始撰寫一些干凈的代碼。你應該使用 promises 方法或 async-await 方法。我希望這段代碼可以幫助你:
beforeTabSwitch: async (tab) => {
if (tab !== 'PAYMENT') {
return false;
}
if (!this.isManualValidated) {
return false;
}
try {
const response = await this.savePayment();
this.placeOrder();
} catch (error) {
console.log(error);
}
return true;
},
savePayment: async function () {
try {
const paymentResponse = await this.$http.post(this.savePaymentRoute);
const summaryResponse = await this.getOrderSummary();
} catch (error) {
console.log(error);
}
},
placeOrder: async function () {
try {
const response = await this.$http.post(this.saveOrderRoute);
} catch (error) {
console.log('placeOrder | ' error);
}
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/484199.html
