I did write an Observable which is polling an URL which completes once a specific value is returned.
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.
}
This works pretty fine but I'm struggling on how to throw an exception once my respone for example hits the following status "r.Status === 'canceled'".
Thanks for any hint on that!
Regards Lukas
You can just use
do()
and throw anError
with whatever condition you need:This prints to console:
In your case you'll want to test a condition such as
r.Status === 'canceled'
or whatever.