Home > Mobile >  Is "catch(console.error)" a valid way of catching Promise rejections?
Is "catch(console.error)" a valid way of catching Promise rejections?

Time:11-28

Let's say I have a Promise and catch the error like so:

fetch().then(data => {
  console.log(data);
}).catch(console.error);

Is this a valid code? I've seen people do this, but I don't understand how catch(console.error) works.

CodePudding user response:

Yes, if you just want to log the error and otherwise ignore it, that works.

.catch expects you to pass it in a function, and if the promise rejects, that function will be called with the rejection value. Often you'll create a new function with custom logic and pass that in, but it's fine to pass in a function that already exists, such as console.error.

CodePudding user response:

yes it's all right.It's same as passing error object and then logging it in console.

  • Related