Я изучаю обещания/машинописные/угловые, и я хочу условно обезопасить себя.Как сделать условное объединение цепей
Это фактическое состояние моего метода:
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();
});
}
}
Я знаком в асинхронном/Await из C#, ни один из которых дает проблемы с условной цепочки, но у меня возникают проблемы в достижении того же с обещаниями.
Является ли это исправить, чтобы поставить .then не только после создания обещания, но после того, как если?
Возможно ли, что .then не может быть вызвано, потому что обещание уже закончено?
Возможно, это было бы лучше для [CodeReview] (http://codereview.stackexchange.com/)? – Script47
@ Script47 не совсем. OP хочет помочь добавить дополнительные функциональные возможности в свой код. Это будет Off-Topic в обзоре кода. – Kaz
@ Zak OP фактически не задал вопрос или не заявил, что у него есть какие-либо проблемы, кроме того, что в заголовке, что звучит точно так же, как запрос обзора кода. – Alnitak