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

251
Views
Observable that delays a random time everytime the stream ends

I need to create a bot that tweets with random intervals, like this:

wait 1 hour
tweets something
wait 5 hours
tweets something
wait 30 minutes
tweets something
wait 10 hours
tweets something

where the delay time is in between 10 hours or something like that

The problem with observables is that I need to generate these intervals and then feed it into the chain of observables. For example, I need to generate an array with all the delays and only them subscribe to it in order to tweet.

Isn't there a way to create an observable that emits a new item just when itself ends? For example:

Rx.Observable.just().timer(randomTime()).subscribe(emitObservableAgain());

I think this is somehow not what Observables are meant to do. Like... is it a good pratice to connect the end of a chain of observables to itself?

ALSO, how to import the rx.timer into nodejs? Importing just 'rx' (from npm) won't give me these functions

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could use repeat:

Rx.Observable.just()
    .switchMap(() => Rx.Observable.timer(randomTime()))
    .do(tweetMyThing)
    .repeat()
    .subscribe();

Edit: I Have added a live-script that shows how the stream works:

// Helper-Methods to visualize the stream
function randomTime() {
    return ~~(Math.random() * 2000) + 50;
}

var lastReceivedEvent = +new Date();
function tweetMyThing(value) {
    console.log("Received after " + (+new Date() - lastReceivedEvent));
    lastReceivedEvent = +new Date();
}
// \end of helper-methods

// the stream
Rx.Observable.just()
    .switchMap(() => Rx.Observable.timer(randomTime()))
    .do(tweetMyThing)
    .repeat(20)
    .subscribe();
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.js"></script>

over 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!