2017-01-02 6 views
2

特定の値が返されると完了するURLをポーリングするObservableを書きました。HTTPリクエストが特定の値を返すと、rxjsがエラーをスローする

private checkPairingStatus(paringModel: any): Observable<ResponseObject> { 
    let data = { id: 1234 }; 
    return Observable 
     .interval(2000) 
     .switchMap(() => this.get<ResponseObject>('http://api/getstatus', data)) 
     .first(r => r.Status === 'success') // once our pairing is active we emit that 
     .timeout(90000, Observable.throw(new Error('Timeout ocurred'))); 
     // todo: find a way to abort the interval once the PairingStatus hits the 'canceled' status. 
} 

これはかなりうまく動作しますが、例えば、私のresponeは、以下のステータス当たったら私は「r.Statusを=== 『キャンセル』」例外をスローする方法について苦労しています。

ありがとうございました!

よろしく ルーカス

答えて

3

あなただけdo()を使用して何が必要条件とErrorを投げることができます。

return Observable 
    .interval(200) 
    .do(val => { 
     if (val == 5) { 
      throw new Error('everything is broken'); 
     } 
    }) 
    .subscribe(
     val => console.log(val), 
     err => console.log('Error:', err.message) 
    ); 

これは、コンソールに出力します。

0 
1 
2 
3 
4 
Error: everything is broken 

あなたのケースでは、あなたが」 r.Status === 'canceled'などの条件をテストしたいですか。

+0

これは動作するようです! – Lukas

関連する問題