Skip to content Skip to sidebar Skip to footer

Get Number Of Open Orders For A Symbol Using Binance's Node.js API

I am using Binance's Node.js API. It says regarding 'Get open orders for a symbol', I should do: binance.openOrders('ETHBTC', (error, openOrders, symbol) => { console.info('op

Solution 1:

You'll need to use either completely async approach or use callbacks.

The last block in your question shows exactly what this answer explains. Javascript doesn't wait for Promise to resolve/reject in a synchronous context. So your "async" block returned the unresolved Promise and the rest of your (synchronous) code didn't wait for it to resolve.

Example of using async functions

const getOpenOrdersCount = async () => {
    const openOrders = await binance.openOrders("ETHBTC");
    return openOrders.length;
};

const run = async () => {
    const openOrdersCount = await getOpenOrdersCount();
    console.log(openOrdersCount);
}

Note: You can use await only within async functions.

Example of using callbacks is your code. They are useful in a small scope, but in bigger scope they get messy and turn into a callback hell. So I wouldn't recommend using callbacks in a bigger scope.

binance.openOrders("ETHBTC", (error, openOrders, symbol) => {
  console.info(openOrders.length);

  // here goes rest of your code that needs to use the `openOrders` variable
});

Post a Comment for "Get Number Of Open Orders For A Symbol Using Binance's Node.js API"