私は基本的にPromisesを返す別のSQLストアドプロシージャを呼び出しています。通常、これらは非同期であるため、ランダムな順序で開始/終了します。私は各手続きが呼ばれる順序を制御する必要があります。プロミス完了を確認し、次にプロミスを実行します。観察可能な対プロミス?
私はcallCustomerIUDの約束を.then()
を使用して試してみましたが、this._dataService.customerIUD(...).then(...)
はcallCustFieldIUD()
後まで実行されませんので、customerFieldIUD
はundefined
としてthis.key
を取得します。
saveChanges(record) {
this.callCustomerIUD(record);
this.callCustFieldIUD();
}
callCustomerIUD(record): Promise<any>{
return this._dataService
.customerIUD(...)
.then(data => {
//THIS KEY IS NEEDED FOR customerFieldIUD
this.key = data[data.length-1].CustomerKey;
}, error => {
console.log(error);
});
}
callCustFieldIUD() : Promise<any>{
//USES KEY FROM customerIUD
this.fillCustomerField(this.key);
return this._dataService.customerFieldIUD(...);
}
私はオブザーバブルを考慮しましたが、この場合でも使用できますか? 上記の私のdata.service.ts
メソッド参照があります。これらはObservablesであり、Promisesではないでしょうか?
customerIUD(data: any) : Promise<any>{
return this.fooHttp
.postData(...);
}
customerFieldIUD(data: any) : Promise<any>{
return this.fooHttp
.postData(...);
}
は、一般的にES6の約束が行う仕事を行うことができ観測します。私はあなたの方法で何が起こっているのかよくわからないが、おそらくRxJSの演算子が役に立つかもしれない。 – estus
私はobservablesを初めて使っています。 Obsvableの 'Promise'型を' Observable'に変更すれば、Observablesにrxjs演算子を使うことができますか? – jhhoff02
'fooHttp'があなた自身または第三者のサービスであるかどうかは不明です。それが約束を返すなら、 'fromPromise'演算子で観測可能に変換するか、' fooHttp'サービス全体を観測値を使うように修正する必要があります。 – estus