Я изучаю promises / typescript / angular и хочу связать обещания условно.
Это фактическое состояние моего метода:
private executePromiseModificationEvenement<T>(edition: Models.CalendrierParametresModelEdition, modeCreation: boolean, deferred: ng.IDeferred<T>) {
var promise: ng.IPromise<Object>;
//Step1
if (modeCreation) {
promise = this.$calendrier.Actions.enregistrerEvenementOutlook(edition);
} else {
promise = this.$calendrier.Actions.modifierEvenementOutlook(edition);
}
if (this.$scope.outlook) {
promise.then((x) => {
if (x != '') edition.idOutlook = x.toString();;
return deferred.resolve();
}, (x) => {
return deferred.reject();
});
} else {
//Step2
promise.then((x) => {
if (x != '') edition.idOutlook = x.toString();
return this.$calendrier.Actions.modifierEvenement(edition);
}, (x) => {
//Ajout MessageBox message error
return this.$calendrier.Actions.modifierEvenement(edition);
})
//Step3
.then((x) => {
if (edition.opportunite != null) this.$rootScope.$broadcast("pushEchangeOpportunite", { idOpportunite: parseInt(edition.opportunite), action: 2, IdContact: edition.id, Libelle: edition.title, StartDate: moment(edition.start).toDate() });
return deferred.resolve();
}, (x) => {
return deferred.reject();
});
}
}
Я знаком с async / await в C #, ни один из которых не вызывает проблем с условной цепочкой, но у меня проблемы с достижением того же с обещаниями.
Правильно ли ставить .then не сразу после создания обещания, а после if?
Возможно ли, что .then никогда не будет вызван, потому что обещание уже выполнено?
return
окончательное значение из любой.then
цепочки [т.е.return promise.then(...)
]. Без этого вся функция немедленно вернется с уже определеннымundefined
значением. - person Alnitak   schedule 19.01.2016