As shown below, there is a function that pulls data from a specific server using a function that uses a promise object called axios.
const getData = async () => {
try {
const result = await axios.get(
"https://b2ef45f8-e166-41d7-bd35-9ad159028fbf.mock.pstmn.io/test"
);
const products = result.data.products;
console.log(products);
console.log(`status:(${result.status}) sucess to connection`);
const data = JSON.stringify(products);
return data;
} catch (err) {
console.error(err);
console.log("failed to get result.");
return err;
}
};
console.log(getData());
it is free to use the value inside the function, When I try to get the value by calling that function in global space, the value Promise { } is first printed to the console, followed by the contents of the function above. I understands that this is because promises are asynchronous. However, I have to use this promise value (data variable) in the global context, so I want to know how to use it in the global context.