Мне нужно получить четыре обещания ответа, но для этого мне сначала нужно вызвать каждую функцию в последовательности от первого до последнего. Из моего кода вы можете видеть, что я вызываю следующую функцию в обратном вызове обещания от последней вызываемой функции.Последовательные обещания
Но код не подходит, поэтому мне нужно знать, есть ли лучший способ сделать это.
Любые предложения?
$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);
});
};
Я не знаю, есть ли у вас контроль над функциями, возвращающими обещания. Если вы это сделаете, я бы подумал о том, чтобы переработать их, чтобы вы могли избежать так много асинхронных вещей. Я всегда рассматриваю красный флаг, если мне приходится много раз гневаться с обещаниями. –