checkout.ts
updateGlobalValue(){
updateShadowDomButton();
let globalValue = fetchGlobalValue()
}
web_component_render.ts
let globalValue;
async fetchData() {
let booleanFromApi = await callToExternalAPI();
return booleanFromApi;
}
function updateShadowDOMButton() {
fetchData.then((booleanFromApi) => {
globalValue = booleanFromApi;
})
}
export function fetchGlobalValue() {
return globalValue;
}
I need to ensure that I retrieve the boolean after the asynchronous call is finished. My assumption was that .then
would guarantee that the block of code in then will be executed after the promise resolves. However, while waiting for the execution of the callToExternalAPI
, it progresses and retrieves the globalValue. The new global value is then updated once the async call is complete. How can we make sure that the asyncFunction call finishes before retrieving the globalValue?