Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Observable polling?

I currently have an Observable timer:

private poller(): Observable<PrepareData> {
    return Observable.timer(0, 5000).switchMap(() => this.http.get("/cgi/dashboard.php")).map(res => res.json());
}

I'd like to have it so a get request is done 5 seconds after the last one completed. Any easy way to do this?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I'll add my answer as @Maxime's answer works, but there is actually no need for a subject.

This is done using the .concat() operator and recursion with the getData() function.

.concat()

Concatenates multiple Observables together by sequentially emitting their values, one Observable after the other.

(...)

concat will subscribe to first input Observable and emit all its values, without changing or affecting them in any way. When that Observable completes, it will subscribe to then next Observable passed and, again, emit its values. This will be repeated, until the operator runs out of Observables. When last input Observable completes, concat will complete as well.

(I used the js version in order to make a snippet that works with stack Overflow's tool, but this is the same for typescript):

function someHTTPCall() {
  // well it's a fake http call
  console.log("making the Http request (can take up to 5s)...");
  return Rx.Observable.of("http response !")
    .delay(Math.round(Math.random() * 5000));
}

function getData() {
  return someHTTPCall()
    .concat(Rx.Observable.timer(5000).switchMap(() => getData()));
}

let myObs = getData();

myObs.subscribe((data) => {
  console.log(data,"waiting for 5 seconds before next request");
});
<script src="https://unpkg.com/rxjs@5.4.0/bundles/Rx.min.js"></script>

about 4 years ago · Santiago Trujillo Report

0

This is an interesting question and I had some fun to come up with a solution.

This solution wait for the previous to finish as you expect :

const { Observable, Subject } = Rx

const getFakeHttpRequest$ = () => Observable.of('response !').delay(3000)

const polling$ = new Subject()

Observable
  // need to tick the first time
  .of(null)
  // everytime our polling$ subject will emit, we'll do again what's next
  .merge(polling$)
  // register to a fake request
  .switchMap(_ =>
    getFakeHttpRequest$()
      .do(_ => {
        // once we're here, the request is done
        // no mater how long it was to get a response ...
        console.log('HTTP request done !');

        // ... we wait 5s and then send a new value to polling$ subject
        // in order to trigger a new request
        setTimeout(_ => polling$.next(null), 5000)
      })
  )
  .subscribe()

Notice that I've put a 3s delay on the request, and that a new request kicks in every 8s, as we waited 3s for the response and then 5s as requested in your question.

Here's a working Plunkr : https://plnkr.co/edit/kXefUbPleqyyUOlfwGuO?p=info

about 4 years ago · Santiago Trujillo Report

0

you can use the interval operator:

private poller(): Observable<PrepareData> {
    return Observable.interval(5000).switchMap(() => this.http.get("/cgi/dashboard.php")).map(res => res.json());
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!