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

292
Visualizações
The second RxJS `scan' operator does not continues from the previous one?

I have the following RxJS code: code example /* The result is: 4 9 15 4 9 15 */ ... why does the second scan start from the beginning (4,9,15) and does not continues from the previous scan (displaying 19,24,30). It is the same stream after all?

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

0

observables does not work like that. You have to see them as a flow of water, where you can change the water with operators. Every time you do something with numbers$ you are starting a new flow of water. So the first pipe has nothing to do with the other pipe and vice versa.

If you want to have return value about the first scan you have to save the return value of the pipe and extend it with an extra pipe.

// 'scan' test
let numbers$ = from([4, 5, 6]);

let val1 = numbers$
  .pipe(
    // Get the sum of the numbers coming in.
    scan((total, n) => {
      return total + n
    }),

    // Get the average by dividing the sum by the total number
    // received so var (which is 1 more than the zero-based index).
    //map((sum, index) => sum / (index + 1))
  )

val1.subscribe(x => console.log('value of the first scan is', x))
val1.pipe(
  scan((total, n) => {
    return total + n
  })
).subscribe(console.log);

Or you can add another scan into the pipe. But then you will lose the value of the first scan:

let numbers$ = from([4, 5, 6]);

numbers$
  .pipe(
    // Get the sum of the numbers coming in.
    scan((total, n) => {
      return total + n
    }),
    scan((total, n) => {
    return total + n
  })
    // Get the average by dividing the sum by the total number
    // received so var (which is 1 more than the zero-based index).
    //map((sum, index) => sum / (index + 1))
).subscribe(x => console.log('value of the second scan is', x))
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