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.