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

156
Visualizações
How to handle errors in rxjs instead of try/catch?

Originally, the error is handled as follows.

async getLists() {
  try {
    const list = await api.getList();
    list.filter() ....
  } catch(e) {
    console.log(e)
  }
}

How can I handle it using rxjs in the same situation?

I'm not familiar with rxjs, so I don't know if it's the answer, but the code I thought of is like this.

getLists() {
 from(api.getList()).subscribe(
   list => list.filter.....,
   e => console.log(e)
 )
}

I want to know how to handle all the errors that may occur when calling the API, such as try-catch, and the errors that may occur when processing the data received from the API after the API call.

Promises are being returned after API calls, but rxjs must be used If an error occurs, I want to unsubscribe.

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

0

So this should be a decent example of how to handle errors.

const getLists = getLists() {
 from(api.getList()).pipe(catchError(error => of(`Bad Promise: ${error}`))
}

//output: 'Bad Promise: Rejected'
const subscribe = getLists.subscribe(val => console.log(val));

This is based on your example. You need to handle a promise, not an observable. (as mentioned by @Aakash Garg).

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use subscribe and handle subscribe error like this:

getLists() {
 api.getList().subscribe(response => {
   // do something with the response data
 },
 () => {
    console.log('Error text');
  })
}

let me know if it works for you.

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