I have a API call in another file that I am importing into my main file. I want to make a synchronous API call because I cannot use useState hook to set data when response is available (because API is in another file...?). Can I pass the useState hook to the API?
I was under the impression that a promise would not get returned because of async/await use.
As of right now a resolved promise is getting returned to the caller file:
import errorList from "../Static-codes/ErrorList";
export const retrieveBasicUserData = async (usernameFieldValue) => {
const responseObject = {
data: null,
error: {
exists: false,
type: null,
},
};
const response = await fetch(
`https://api.github.com/users/${usernameFieldValue}`
);
const responseJSON = await response.json();
if (response.status == 404) {
responseObject.error.exists = true;
responseObject.error.type = errorList.userNotFoundError;
} else if (response.status === 200) {
responseObject.data = responseJSON;
} else if (response.status !== 200 && response.status !== 404) {
responseObject.error.exists = true;
responseObject.error.type = errorList.apiGeneralError;
}
console.log(responseObject);
return responseObject;
};
let responseObject = retrieveBasicUserDataAPI(usernameFieldValue);
console.log(responseObject);