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.
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.
yes it's all right.It's same as passing error object and then logging it in console.
fetch().then(console.log).catch(console.error);or justfetch().then(console.log, console.error);