In Nodejs v16.15.1, I can easily assign axios respond to a variable.
const clientlist = await axios.get('http://test.lab/api/getclients');
However, when I upgraded to latest Nodejs v19.6.0, I keep getting error: await is only valid in async functions.
So, I tried to change the code as per below
var uid1 = 0;
async function getUID() {
let res = await axios.get('http://test.lab/api/?user=test');
let data = res.data;
return data;
}
// Call start
(async() => {
console.log('before start, the value is '+ uid1);
uid1 = await getUID1();
console.log('after start, the value is '+ uid1);
})();
//trying to use uid1 for the rest of the code
console.log('outside async, the value is '+ uid1);
// returned empty
However, I can only use axios respond within the async scope. I cannot use it outside.
What can I do to make it work like it did in nodejs v16?