form
/component
があります。これは、「保存」を行うために4つの別々のAPI呼び出しを行う必要があります。本当に、それらのトランザクションのうちの1つだけが他の3つを待つ必要がありますが、私は4つすべてを安全にするために連鎖したがっていました。私はこれらの関数が実行されます(と応答が戻ってくる)ことを正確に順序を知っているので、私はログメッセージを持っているものをsave{number}
機能の各内部角2私の4つのapi呼び出しが前の呼び出しが実行前に終了するのを待っているのはなぜですか?
save() {
this.save1(saveObject1)
.subscribe(response => {
if (response === 0) {
this.processErrorResult('save1');
}
this.save2(saveObject2)
.subscribe(response2 => {
if (response2 === 0) {
this.processErrorResult('save1');
}
this.save3(saveObject3)
.subscribe(response3 => {
if (response3 === 0) {
this.processErrorResult('save1');
}
if (this.noErrorsInFirst3Saves()) {
this.save4(saveObject4)
.subscribe(response4 => {
if (response4 === 0) {
this.processErrorResult('save1');
}
if (!this.hasErrors()) {
this.router.navigate(['confirmation']);
}
});
}
});
});
});
}
private save1(saveObject: any): Observable<int> {
this.api.save1(saveObject)
.subscribe(successful => {
return Observable.of(1);
}, failed => {
return Observable.of(0);
});
}
private save2(saveObject: any): Observable<int> {
this.api.save2(saveObject)
.subscribe(successful => {
return Observable.of(1);
}, failed => {
return Observable.of(0);
});
}
private save3(saveObject: any): Observable<int> {
this.api.save3(saveObject)
.subscribe(successful => {
return Observable.of(1);
}, failed => {
return Observable.of(0);
});
}
private save4(saveObject: any): Observable<int> {
this.api.save4(saveObject)
.subscribe(successful => {
return Observable.of(1);
}, failed => {
return Observable.of(0);
});
}
:ここ
は、私はそれを設定している方法です。
私は[保存]をクリックすると、それは[ほとんど]すぐにその後、私はそこに座って、クロムのdevのツールで私のコンソールウィンドウを見て、APIコールからの応答がでてくるし始める見ることができ、私の確認ページにリダイレクトします。
これらのトランザクションの明示的な連鎖を受け付けないのはなぜですか?私はAngular
で知っています。promise
でこれを行うのはとても簡単で、.then(response => {});
です。私はそれを考えることができ
唯一のものは異なっている、それはget
/post
/put
/delete
のかどうか、EVERY API呼び出し用で、それは常にクロムのdevのツールのネットワークパネルの2つの呼び出しを示しています。最初のコールはRequestMethod
がOptions
に設定され、その後のコールは対応するget
/post
/put
/delete
となり、それに応じてRequestMethod
が設定されます。
私が最近取り組んだ最近のアプリケーションの中で(複製された呼び出しを)見たことがないのですが(おそらく私はそれほど注意を払っていないかもしれません)、APIの標準操作手順ですコール。
ここに任意のアイデアはありますか?
編集:は
以下ハリーの提案の回答を受けて、私はあなたの答えを実装しようとしているが、実装方法のまだ少し混乱しては、ここに私のセットアップを詳しく見です:
private save1(saveObject: any): Observable<number> {
if (saveObject.hasChanges()) {
return this.api.save1(saveObject)
.map(successful => {
return this.parseSaveResponse(successful, saveObject, true); // true indicates a successful API response
})
.catch(failed => {
return this.parseSaveResponse(successful, saveObject, false); // false indicates a successful API response
});
} else {
return Observable.of(-1); // no changes necessary, return immediately
}
}
private parseSaveResponse(response: any, saveObject: any, transactionSuccessful: boolean) {
this.logService.logTransaction({transactionType}, saveObject, response, transactionSuccessful);
if (!transactionSuccessful) {
// add to error list object to be displayed on the form
return 0;
}
return 1;
}
この時点にそれを取得する場合、それは言って.catch({});
ラインで私にエラーを投げています:
Argument of type '(error: any) => 0 | 1' is not assignable to parameter of type '(err: any, caught: Observable<0 | 1>) => ObservableInput<{}>'. Type '0 | 1' is not assignable to type 'ObservableInput<{}>'. Type '0' is not assignable to type 'ObservableInput<{}>'.)
あなたのコールチェーンにrxjsでswitchmap変換演算子に見えます。 Observableを入力として受け取り、それを使って他のAPIを呼び出すことができます。 Observable.forkJoinのメソッド – DOMZE