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

288
Visualizações
Emit values from Observable.from on an interval

I have an array of items that I want to emit from an Observable on an interval.

I am using the rxjs library included in Angular 2.

This is my current solution but I feel like there is a reactive method that I am not finding. This solution also continues to produce values after the array is exhausted but it should really complete. Is there some way to drive Observable.from with Observable.interval?:

var array = [1,2,3];

var source = Rx.Observable
    .interval(500 /* ms */)
    .map(function() {
      return array.pop();
    });

var subscription = source.subscribe(
    function (x) {
        console.log('Next:', x);
    },
    function (err) {
        console.log('Error: ' + err);   
    },
    function () {
        console.log('Completed');   
    });

Output:

"Next:"
3
"Next:"
2
"Next:"
1
"Next:"
undefined
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

For something simple like that, what you've done is fine except that I'd rather use the index given from interval to read the array at that given index instead of using pop which modify the original array (mixed with reactive programming it's kind of weird, isn't it =) !)

I made you another proposal so you may discover other operators maybe :

const { Observable } = Rx

const myArray = [1, 2, 3]

const interval$ = Observable.interval(500)

Observable
  .from(myArray)
  .zip(interval$)
  .do(([x]) => console.log(x))
  .subscribe(
    () => {},
    () => {},
    () => console.log('Stream ended !')
  )
<script src="https://unpkg.com/rxjs@5.1.1/bundles/Rx.min.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