Reputation: 1
I am trying to fetch the gas prices from ethgasstaion and work with this value in the global scope. Is there a way that I can include a global variable in my .then() part and assign the value of my async function to that variable?
I get the right value but can't assign it to the global variable.
// Eth Gas Station
let fast_value = 0;
async function getCurrentGasPrices() {
let response = await axios.get(process.env.ETH_GAS_Station_API)
let price = {
fast: response.data.fast
}
return price
}
getCurrentGasPrices().then(value => {
fast_value += value.fast;
})
console.log(fast_value)
Appreciate the help!
Upvotes: 0
Views: 71
Reputation: 100
You can wrap everything into an immediately invoked async function, so we can use await inside:
(async () => {
let fast_value = 0;
async function getCurrentGasPrices() {
let response = await axios.get(process.env.ETH_GAS_Station_API);
let price = {
fast: response.data,
};
return price;
}
let response = await getCurrentGasPrices();
fast_value = response.fast;
console.log(fast_value);
})();
Upvotes: 1