let data;
async function getData()
{
let response = await fetch('http://~~~');
data = await response.json();
return data;
}
getData().then(data => console.log("bbbbb",data));
console.log("aaaaa",data);
this is my javascript code. I want to know how to receive data as a global variable. (bbbbb is successd, but aaaaa is not sucessed)
let globalData;
async function getData()
{
let response = await fetch('http://~~~');
globalData = response.yourData;
}
getData();
// even you dont need this thing to do.
// getData().then(data => console.log("bbbbb",data));
console.log("aaaaa", globalData);
By this, globalData
variable is global now. and you can get data by globalData
.
let data;
async function getData()
{
let response = await fetch('http://~~~');
data = await response.json();
return data;
}
getData()
console.log("aaaaa",data);
Anything outside you function scope can be considered as global. Here data is a global variable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.