Thanks for the help, in the end I had to rewrite all the code of the klines method achieving the task in addition to saving myself some lines of code, this is the result
function klines(symbol = 'BTCUSDT', interval = '1d', limit = 1000, startTime = null, endTime = null, callback = () => {}) {
return new Promise(async (resolve, reject) => {
var data, url, klArray = [],
endpoint = '/api/v3/klines';
while(limit > 0){
url = burl + endpoint + '?symbol=' + symbol + '&interval=' + interval;
url += '&limit=' + (limit > 1000 ? 1000 : limit);
url += '&startTime=' + (startTime ? startTime : (endTime ? new Date(endTime) : Date.now()) - (intervals[interval].ms * limit));
url += endTime ? (limit > 1000 ? '' : '&endTime=' + endTime) : '';
data = await loadRequest('GET', url, )
klArray.length != 0 ? klArray = klArray.concat(data) : klArray = data;
startTime = data[data.length - 1][0] + intervals[interval].ms;
limit -= 1000;
sleep(500)
}
callback(klArray)
resolve(klArray)
})
}
function loadRequest(method, url, callback = () => {}) {
return new Promise(async (resolve, reject) => {
try{
var response = await fetch(url, {method:method})
if(response.ok){
var data = await response.json()
callback(data)
resolve(data)
}
else{
return reject(response)
}
}
catch(e){
setTimeout("loadRequest('" + method + "', '" + url + "', " + callback + ")", 30000)
return reject(e)
}
})
}
and to call it I stay like this
async function plotCharts(){
await klines(symbol, interval, limit, null, Date.now() - intervals[interval].ms, plotCandles)
kline(symbol, interval, plotLastCandle),
depth(symbol, 5000, plotOrderBook)
}
In fact, kline and depth had them put inside a Promise.all but measuring the execution time took much longer than putting the code without the Promise, it seems strange to me
async function plotCharts(){
await klines(symbol, interval, limit, null, Date.now() - intervals[interval].ms, plotCandles)
await Promise.all([
kline(symbol, interval, plotLastCandle),
depth(symbol, 5000, plotOrderBook)
])
}
If you have any idea why it takes longer within the Promise.all, I would appreciate the answer.