Search code examples
node.jsasynchronoustradingcryptocurrency

Run other code while awaiting


This is probably a stupid question, but my current project is stuck until I figure it out.

Say I have this function.

async function AtoB() {
   try{
      await ACheck();
      let quantity = (ABalance - (1 / ABuy));
      quantity = parseFloat(quantity.toFixed(4));
      exchange.createMarketBuyOrder('A/B', quantity)
      trade();
   } catch(err) {
      console.log(err);
   }
}

The function is awaiting another function called ACheck() that looks like this:

async function ACheck() {
   while(ABalance/ABuy < 10) {
   }
   return true;
}

My issue is that ABalance and ABuy are set in another function that gets their values using CCXT. When AtoB() runs, it stops a setTimeout I have in place that calls the price function every 5 seconds. This effectively means that the function will never run since the values for ABalance and Abuy never update.

Is there any way to run the prices function at the same 5 seconds intervals while awaiting ACheck()?

Thanks in advance


Solution

  • You are not allowed to block the main thread if you want to run other things async.

    Just have your setTimeout run like it does now, when it fires, calculate ABalance/ABuy. If Abalance/Abuy < 10, do nothing and wait for next setTimeout to fire. If not, do the rest.