Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda