I am trying to understand JavaScript, specifically fetch. I have the following code but it is not making sense to me because console.log(responseObject)
prints data but console.log(res)
prints undefined.
How is that possible? Why is responseObject
not getting passed into res
?
I do not want to mess around with async/await
keyword as I find .then
to be a lot cleaner.
export const retrieveBasicUserData = (usernameFieldValue, setStateFunction) => {
const responseObject = {
data: null,
error: null,
};
fetch(`https://api.github.com/users/test`)
.then((response) => {
response.json().then((data) => {
responseObject.data = data;
console.log(responseObject);
return responseObject;
});
})
.then((res) => console.log(res));
};