I need to obtain a value from an API which is actualized quickly. The problem is that the code I wrote bring me always the same value. I tried two ways:
var fetch = require("node-fetch");
for(let i=0; i<5; i++){
setTimeout(function(){}, 3000);
fetch('https://api.binance.com/api/v3/avgPrice?symbol=ETHBTC')
.then(res => res.json())
.then(data => {console.log(data.price)});
}
and in a synchronous fashion:
var fetch = require("node-fetch");
var request = async () => {
var response = await fetch('https://api.binance.com/api/v3/avgPrice?
symbol=ETHBTC');
var json = await response.json();
console.log(json.price);
}
for(let i=0; i<5; i++) {
setTimeout(function(){request();}, 3000);
}
but I always obtain something like this:
0.03244206
0.03244206
0.03244206
0.03244206
0.03244206
Any suggestion?