I had a problem how to return axios status from function. I have found the solution but I feel there is a better option. I have tried many ways but only this one works. Is this solution correct? or I should find another?
export const getPerson = async (patient) => {
try {
const res = await axios.post('/api/user',patient)
return res.data
} catch (err) {
return err.response.status
}
};
onSubmit = async () =>{
await getPerson({id:this.state.id})
.then(data => {
if(data === 404){
this.setState({errorStatus:data})
}
console.log(data)
})
}
Also Can someone explain to me why in this example then and catch execute simultaneously.
onSubmit = async () =>{
await getPerson({id:this.state.id})
.then(data => {
console.log(data)
})
.catch( err => console.log(err)
}