Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda