2017-01-02 107 views
2

我確實編寫了一個Observable,它正在輪詢返回特定值後完成的URL。rxjs在http請求返回特定值時拋出錯誤

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. 
} 

這工作得很好,但我掙扎於如何一次我的輸入反應例如點擊下面的狀態「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

tnx這似乎工作! – Lukas

相關問題