I am trying to load an array of memberdata into a javascript variable so that I can operate on it from within several different functions. After giving it the old college try, I am lost.
I understand the need for asynchronous functions so as not to block the main thread but the only way I have been able to populate a global variable is by storing the function result to session.Storage and then calling it from there in other functions. While this works it seems like an extra step.
async function fetchMember($vid) {
let options = {
method: 'POST',
async:false,
body: JSON.stringify({vid: $vid}),
headers: {
'Content-Type': 'application/json', // sent request
'Accept': 'application/json' // expected data sent back
},
}
let response = await fetch('getMember.php', options);
let $member = await response.text();
sessionStorage.setItem("member", $member)
return ($member); // this is a promise because it is in an async function
} //end of function
fetchMember("999999").then ($dog=> console.log ("Then = " + $dog))// $dog is a promise
console.log ("Member = " + $member); // undefined
$member = session.Storage ("member") // work as expected
I can ".then" log the result of the promise to the console but for the life of me I can't figure out how to store it in a global variable.