I'm new to react native and I'm trying to write an async function which calls an API endpoint that takes an id and returns the result of the API from the function.
Here is an example snippet of my code:
async function returnAValue(funcId) {
let returnValueFromAPI = false;
if (funcId !== null) {
try {
const response = await Api.get(
"/metrics-result/hasRecordedToday/" + funcId
);
returnValueFromAPI = response.data;
console.log("returnValueFromAPI 1", returnValueFromAPI);
// here the correct value of true is logged "returnValueFromAPI 1 true"
} catch (error) {
console.log(error.response);
}
}
return returnValueFromAPI;
}
console.log("func return value", returnAValue("3456"));
// outside the function above console log returns 'func return value' {"_U": 0, "_V": 0, "_W": null, "_X": null}
Can someone explain why it doesn't return the 'true' value in the console.log outside the function ?
Any help would be appreciated