I have an api call. In case it fails, I want to get an error. Why does the error appears in the then() and not in the catch()? How to access it from catch? I'd prefer not having to write a long function inside of the then() such as if !res &&, etc.
Here is the code:
const postStuff = async () => {
try {
const res = await ax.post("/stuff");
return res;
} catch (err) {
return new Error();
}
};
export default function App() {
React.useEffect(()=> {
postStuff()
.then(res=> console.log("res", res))
.catch(err=> console.log("err", err))
}, [])
return (
<div className="App">
hello world
</div>
);
}