rxjs在http请求返回特定值时抛出错误
问题描述:
我确实编写了一个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 ===‘取消’”抛出异常。
感谢您的任何提示!
问候 卢卡斯
答
你可以只使用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'
或其他。
tnx这似乎工作! – Lukas