2016-09-27 6 views
0

私は基本的にPromisesを返す別のSQLストアドプロシージャを呼び出しています。通常、これらは非同期であるため、ランダムな順序で開始/終了します。私は各手続きが呼ばれる順序を制御する必要があります。プロミス完了を確認し、次にプロミスを実行します。観察可能な対プロミス?

私はcallCustomerIUDの約束を.then()を使用して試してみましたが、this._dataService.customerIUD(...).then(...)callCustFieldIUD()後まで実行されませんので、customerFieldIUDundefinedとして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(...); 
} 
+1

は、一般的にES6の約束が行う仕事を行うことができ観測します。私はあなたの方法で何が起こっているのかよくわからないが、おそらくRxJSの演算子が役に立つかもしれない。 – estus

+0

私はobservablesを初めて使っています。 Obsvableの 'Promise'型を' Observable'に変更すれば、Observablesにrxjs演算子を使うことができますか? – jhhoff02

+1

'fooHttp'があなた自身または第三者のサービスであるかどうかは不明です。それが約束を返すなら、 'fromPromise'演算子で観測可能に変換するか、' fooHttp'サービス全体を観測値を使うように修正する必要があります。 – estus

答えて

1

callCustFieldIUD()関数は(購読の内部で呼び出されているのではい、観測がに)このシナリオ

saveChanges(record) { 
    this.callCustomerIUD(record).take(1).subscribe((data: any) => { 
    // Observables can be subscribed to, like a .then() on a promise 
    // data will be the response from the http call 
    this.callCustFieldIUD(data).take(1).subscribe(); 
    }); 
} 

callCustomerIUD(record): Observable<any>{ 
    return this._dataService.customerIUD(...) 
} 

callCustFieldIUD(data: any) : Observable<any>{ 
    //USES KEY FROM customerIUD 
    this.fillCustomerField(this.key); 
    return this._dataService.customerFieldIUD(...); 
} 

とサービスで

customerIUD(data: any) : Observable<any>{ 
    return this.fooHttp.postData(...).map((res: any) => { 
    return res.json(); 
    }); 
} 

customerFieldIUD(data: any) : Observable<any>{ 
    return this.fooHttp.postData(...).map((res: any) => { 
    return res.json(); 
    }); 
} 

に素晴らしいことですcallCustomerIUD()関数によって返されたオブザーバブルは、データがそこに来るまで実行されません。

(これはあなたが必要とする正確なコードではないかもしれない。私は場合に発生しているかわからないんだけど、観測に加入することで、あなたは関数が呼び出されるときの詳細厳しいことができます)

この情報がお役に立てば幸いです

EDIT _________________________私はあなたがあまりにも約束してこれを実現することができると信じて

は、ちょうどわずかなリファクタリングを必要と_______________

saveChanges(record) { 
    this.callCustomerIUD(record); 
} 

callCustomerIUD(record): Promise<any>{ 
    return this._dataService 
     .customerIUD(...) 
     .then(data => { 
      // Instead of setting an instance var here, pass it in to the callCustFieldIUD() function 
      this.callCustFieldIUD(data[data.length-1].CustomerKey); 
     }, error => { 
      console.log(error); 
     }); 
} 

callCustFieldIUD(customerKey: any) : Promise<any>{ 
    //USES KEY FROM customerIUD 
    this.fillCustomerField(this.key); 
    return this._dataService.customerFieldIUD(...); 
} 
+1

@ jhhoff02私はそこにも有望な解決策があると思います。上記の私の編集を参照してください –

+0

私はObservablesのアイデアを上記のestusのようなfromPromise()と一緒に使用しました – jhhoff02

1

あなたの最初のバージョンがうまくいかない理由は、あなたのプログラムがhttpリクエストが進行している間も動作し続けるからです。解決策は、必要なキーが設定された後にcallCustFieldIUDの実行を強制的に行うことです。 Observablesを使用している場合、この同じ問題が発生し、同様の方法で解決されます。

あなたが行うことができるはず:

saveChanges(record) { 
    this.callCustomerIUD(record); 
} 

callCustomerIUD(record): Promise<any>{ 
return this._dataService 
    .customerIUD(...) 
    .then(data => { 
     //THIS KEY IS NEEDED FOR customerFieldIUD 
     this.key = data[data.length-1].CustomerKey; 
     // Call the method you need to execute after the key is guaranteed 
     // to be set 
     this.callCustFieldIUD(); 
    }, error => { 
     console.log(error); 
    }); 
} 
関連する問題