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

248
Visualizações
can you catch the rxjs filter like catching an either left

I would like to know if it's possible to catch when the condition of the rxjs filter condition isn't true.

this is what I have:

    of(1)
    .pipe(
      map((d) => d + 1),
      filter((d) => d === 0),
      map((d) => d + 1), // this will go in because of the filter
    )
    .toPromise()
    .then((d) => console.log(d)) // display indefined
    .catch(() => console.log("ERRRRRROOOOOOR")) // do not display :( 

In this case, I would like to return a specific data (or at least throw) when the filter condition isn't true.

I tried to add parameters to the filter

filter(filter((d) => d === 0), "john doe")

But this doesn't display the string,

I guess the equivalent in fp-ts is to catch the either left but I don't know if you can do functional programing with rxjs or if you can only work with observable. The fact that you can using pipe and multiple operator enable the use of this library in my project so it will be good to avoid changing the library

Thanks a lot !

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

0

You need to throw an error to be able to catch it !

of(1)
  .pipe(
    map((d) => d + 1),
    switchMap((d) => {
      if (d === 0) {
        return of(d + 1);
      }
      throwError(() => new Error('Erroooooor'));
    })
  )
  .toPromise()
  .then((d) => console.log(d)) // display indefined
  .catch(() => console.log('ERRRRRROOOOOOR')); // thrown if d !=== 0

It could also be written using the iif operator

of(1)
  .pipe(
    map((d) => d + 1),
    switchMap((d) =>
      iif(
        () => d === 0,
        of(d + 1),
        throwError(() => new Error('Erroooooo'))
      )
    )
  )
  .toPromise()
  .then((d) => console.log(d)) // display indefined
  .catch(() => console.log('ERRRRRROOOOOOR')); // thrown if d !=== 0
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