Help with asynchronous functions

I am trying to make the following code run asynchronously in the same order, klines should be run first and when it finishes loading kline is run and last depth but the following error appears: Cannot read property then of undefined

the code is the following:

	function plotCharts(){
		klines(symbol, interval, limit, null, Date.now() - intervals[interval].ms, plotCandles)
		.then(() => kline(symbol, interval, plotLastCandle))
		.then(() => depth(symbol, 5000, plotOrderBook));
	}

I still have a hard time understanding this about asynchronous functions

This not really has anything to do with API, but some ideas that may helpful:

Is this by Nodejs?


// make sure this method returns promise
const kinePromise = kline(symbol, interval, plotLastCandle);

const depthPromise => depth(symbol, 5000, plotOrderBook);

Promise.all([kinePromise, depthPromise]).then((results) => {

 // you all get both kline and depth result in this results
  console.log(values);
});

1 Like

Nice reply.

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.

Promise.all return only when all promises has final status, maybe one of them blocked? I don’t know the details of each method, but have a try to test each of them in Promise.all