0

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.

1
  • for your code getlists seems to be returning a promise not observable. Commented May 18, 2022 at 15:42

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

2 Comments

If this is done, const subscribe = getLists.subscribe(val => console.log(val))); how does this process handle errors that occur inside the subscribe function? @ZombieChowder
@sjfklsaf you can handle the errors however you want inside the subscribe function, in the given example we just log it with console.log(val). You can pass a function to do something else, or fire a logger or something. I suggest you read through the documentation I've linked in my answer.
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.

Comments

0

It seems your code is returning a promise, you can convert the promise to observable and subscribe to it as shown below.

If the API call is successful it will go to the next block and if any error occurs it will go to the error block.

Note :- defer allows you to create an Observable only when the Observer subscribes. It waits until an Observer subscribes to it.

defer docs - https://rxjs.dev/api/index/function/defer

const { defer, from } = require("rxjs")
    
    async function getLists() {
        return await fetch("https://jsonplaceholder.typicosde.com/users").then(response => response.json())
    }
    
    const observableFromPromiseFactory = defer(() => from(getLists()));
    
    observableFromPromiseFactory.subscribe({
        next: value => console.log(value),
        error: err => console.error('Error->: ', err),
        complete: () => console.log('Completed')
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.