How do i execute the following scenario in the browser with RxJs:
Angular / typescript rewritten solution from above:
export interface PollOptions {
interval: number;
timeout: number;
}
const OPTIONS_DEFAULT: PollOptions = {
interval: 5000,
timeout: 60000
};
@Injectable()
class PollHelper {
startPoll(
pollFn: () => Observable, // intermediate polled responses
stopPollPredicate: (value: T) => boolean, // condition to stop polling
options: PollOptions = OPTIONS_DEFAULT): Observable {
return interval(options.interval)
.pipe(
exhaustMap(() => pollFn()),
first(value => stopPollPredicate(value)),
timeout(options.timeout)
);
}
}
Example:
pollHelper.startPoll(
() => httpClient.get(...),
response => response.isDone()
).subscribe(result => {
console.log(result);
});