For #RxJS version 5+:
You can use the rxjs interval operator and do the polling. The following execution will execute the line this.statusService.getStatus() for each 1000 ms interval:
getList() { return Observable.interval(1000).startWith(1) .mergeMapTo(this.http.get(this.apiURL)) .catch(this.handleError); }
When adding startWith(1) it will now be executed immediately without any delay, after which it will be executed every 1 second. I think this is what you want.
Or another approach: you can use the timer operator and do the following:
getList() { return Observable.timer(0, 1000) .mergeMapTo(this.http.get(this.apiURL)) .catch(this.handleError); }
In this approach, the timer statement will execute instantly, and after that it will be executed every 1000 ms.
Also do not forget to import:
import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/startWith'; import 'rxjs/add/operator/mergeMapTo'; import 'rxjs/add/observable/timer';
source share