1

I'm currently using https://github.com/pixtron/bybit-api/tree/master

and what im trying to do looks like this:

const {RestClient} = require('@pxtrn/bybit-api');

const API_KEY = 'xxx';
const PRIVATE_KEY = 'yyy';

const client = new RestClient(API_KEY, PRIVATE_KEY);

client.getPosition({symbol: 'BTCUSD'})
  .then(msg => {
    let btcPositionCheck = msg.result.size;
    if (btcPositionCheck == 0) {
      console.log("empty")
    } else {
      let btcPositionSize = btcPositionCheck;
      let btcPositionSide = msg.result.side;
    }
  })
  .catch(err => {
    console.error(err);
  });


client.getPosition({symbol: 'ETHUSD'})
  .then(msg => {
    let ethPositionCheck = msg.result.size;
    if (ethPositionCheck == 0) {
      console.log("empty")
    } else {
      let ethPositionSize = ethPositionCheck;
      let ethPositionSide = msg.result.side;
    }
  })
  .catch(err => {
    console.error(err);
  });

console.log(btcPositionSize + ' ' + ethPositionSize)

returning me the following error:

ReferenceError: btcPositionSize is not defined

simply because im out of scope, I'm aware. however, I'm unsure how to approach rewriting the following to better allow me to construct what I need. (I need to call getPosition for the 3 different assets as well and take the variables from each call to use later in my code.) if that makes any sense. help would be greatly appreciated

edit:

async function getPosition({symbol: BTCUSD}) {
  try {
    let btcPositionCheck = msg.result.size;

  } catch(error) {
    return null;
  }
}


im trying to rewrite it but im unsure how

erkster23
  • 17
  • 4
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Shubham Dixit Apr 28 '20 at 11:38

1 Answers1

1

Once you get into asynchronous context, you need to stay in asynchronous context to get values. In your case, there are promises to be resolved, you can await them and then print.

Also you need to make sure that your variables are in valid scope

const {RestClient} = require('@pxtrn/bybit-api');

const API_KEY = 'xxx';
const PRIVATE_KEY = 'yyy';

const client = new RestClient(API_KEY, PRIVATE_KEY);

async function doItAll() {

  let btcPositionCheck;
  let ethPositionCheck;

  await client.getPosition({symbol: 'BTCUSD'})
    .then(msg => {
      btcPositionCheck = msg.result.size;
      if (btcPositionCheck == 0) {
        console.log("empty")
      } else {
        let btcPositionSize = btcPositionCheck;
        let btcPositionSide = msg.result.side;
      }
    })
    .catch(err => {
      console.error(err);
   });


  await client.getPosition({symbol: 'ETHUSD'})
    .then(msg => {
      ethPositionCheck = msg.result.size;
      if (ethPositionCheck == 0) {
        console.log("empty")
      } else {
        let ethPositionSize = ethPositionCheck;
        let ethPositionSide = msg.result.side;
      }
    })
    .catch(err => {
      console.error(err);
    });

    console.log(btcPositionSize + ' ' + ethPositionSize)
}

Note that nicer way is to await promises into value, which means it can look like that (to use await, it needs to be in an async function)

    const msg = await client.getPosition({symbol: 'BTCUSD'})
    btcPositionCheck = msg.result.size;
    if (btcPositionCheck == 0) {
      console.log("empty")
    } else {
      let btcPositionSize = btcPositionCheck;
      let btcPositionSide = msg.result.side;
    }
libik
  • 22,239
  • 9
  • 44
  • 87