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

182
Visualizações
Combining observables that will emit differing number of values

I have 2 observables that are streaming data as a result of a database query, the amount of data each emits is variable and can be different to the other one. I'm trying to work out how I can combine them both so I can use a single subscription and push the emitted values into two different arrays, e.g.

let firstArray = [];
let secondArray = [];

const subscription = zip(firstObservable, secondObservable)
   .subscribe({
      next([first, second]) {
         firstArray.push(first);
         secondArray.push(second);
      },
      error(err) {
         console.log(err);
      },
      complete() {
         console.log(firstArray);
         console.log(secondArray);
      }
   });

The problem with this example is that using zip causes them to only capture emitted values from the shortest observable, so if firstObservable is emitting 2 values and secondObservable is emitting 10, on complete both arrays will be of length 2. I apologise if the question is ambiguous at all, I'm quite new to using observables and am struggling to entirely grasp them. Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use toArray to collect all of the emitted items into an array, and forkJoin to wait for both observables to finish:

import { of, forkJoin } from 'rxjs';
import { toArray } from 'rxjs/operators';

const o1 = of(1,2,3,4,5);
const o2 = of('a', 'b');
forkJoin([
  o1.pipe(toArray()),
  o2.pipe(toArray())
]).subscribe(console.log);

Stackblitz

about 4 years ago · Juan Pablo Isaza 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