我需要得到四個響應的承諾,但要做到這一點,我首先必須按順序調用每個函數,從第一個到最後一個。您可以從我的代碼中看到,我從最後一次調用的函數的promise回調中調用下一個函數。順序承諾
但代碼看起來不合適,所以我需要知道是否有一些更好的方法來做到這一點。
有什麼建議嗎?
$scope.createPayment = function() {
var dados = $scope.card;
// get first promise
PagamentoMP.createToken(dados)
.then(
function(result) {
dados.token = result;
// get second promise
PagamentoMP.guessPaymentMethod(dados)
.then(
function(result) {
dados.paymentMethod = result;
// get third promise
PagamentoMP.getIssuers(dados)
.then(
function(result) {
dados.issuer = result;
// get fourth promise
PagamentoMP.getInstallments(dados)
.then(
function(result) {
dados.installments = result;
},
// error for fourth promise
function(result) {
console.log("getInstallments PAGAMENTOMP -> Failed to get the name, result is " + result);
}
);
},
// error for third promise
function(result) {
console.log("getIssuers PAGAMENTOMP -> Failed to get the name, result is " + result);
});
},
// error for second promise
function(result) {
console.log("guessPaymentMethod PAGAMENTOMP -> Failed to get the name, result is " + result);
});
},
// error for first promise
function(result) {
console.log("createToken PAGAMENTOMP -> Failed to get the name, result is " + result);
});
};
我不知道你是否有任何控制返回承諾的函數。如果你這樣做,我會考慮改造它們,這樣你可以避免這麼多的異步的東西。如果我必須不止一次地用承諾套巢,我總是在考慮一面紅旗。 –