So I got this to work in my create-react-app but I don't quite understand why. My code is as follows:
/*
in App.js
Maybe not relevant but I update someParamState in another component
*/
function App() {
const [ someParamState, setSomeParamState ] = useState(() => '');
const [ someMoreState, setSomeMoreState ] = useState([]);
useEffect(async () => {
const result = await someRequest(someParamState);
setSomeMoreState(result);
}, [someParamState])
console.log(someMoreState);
}
// in API.js
export const someRequest = async (param) => {
let endpoint = `https://some/endpoint}`;
const res = await axios.get(endpoint);
return res.data;
}
If I skip the await in App.js, I will log:
Promise {<pending>}
Promise {<fulfilled>: {…}}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Object
... and I can see I have the data in the [[PromiseResult]]: Object
.
But if I keep the await in App.js, I will log the json object.
So my question is, why do I have to await the axios call twice to get the data directly? I thought I should have the data already after resolving the promise at the first await call in API.js. Also, how would this example be different with the use of .then()?