I know an async/await function only returns a promise, and that I need to add a .then() to update a variable or a state with the value. But in a React app, it obliges me to perform a useEffect and then to update a state. If I return a formatted object such as: {loading, error, data}
, would it be possible to directly access it this way:
const {loading, error, data} = fetchData()
How would you allow direct access to an object from such a function:
async function foo(){
try{
const res = await api("/user/foo")
return {
loading:false,
error:false,
data: res.data
}
}
catch{
return trhow new Error()
}
}