I’m trying to send a request to an api using axios and store the json response in a variable. When I output the response to the console, it shows it, but when setting a variable equal to it and returning said variable, I only get Promise { } as an output.
const { default: axios } = require("axios");
async function getAvgPrice() {
var data = []; // or whatever we would have to declare for a json object
await axios.get("https://api.com/example.json")
.then(function(res) {
console.log(res); // <-- This outputs the correct response to console
data.push(res); // <-- This just doesn't do anything
});
return data;
}
var x = getAvgPrice();
console.log(x); // <-- (Output: Promise { <pending> })
I tried to send a request to an api and store the response as json and am only able to return a promise.