n0mad01 / node.bittrex.api

No longer maintained
MIT License
183 stars 187 forks source link

Unable to make asynchronous requests #49

Closed mhuggins closed 7 years ago

mhuggins commented 7 years ago

I tried to wrap this API with promises so that I can use async/await. I'm trying to hit the same API endpoint with different params in these calls. Unfortunately, the same endpoint is hit repeatedly due to the request_options object being shared between calls.

n0mad01 commented 7 years ago

can you describe that more precisely and maybe add a example?

mhuggins commented 7 years ago

Definitely, sorry for the lack of detail.

I've got a wrapper set up like this:

client.js

import _ from "lodash";
import bittrex from "node.bittrex.api";

bittrex.options({ /* ... */ });

const FUNCTIONS = [
  "getmarkets",
  "getcurrencies",
  /* snip */
];

export default _.zipObject(FUNCTIONS, FUNCTIONS.map((fn) => {
  return (...args) => {
    return new Promise((resolve, reject) => {
      bittrex[fn](...args, (data) => {
        return data.success ? resolve(data.result) : reject(new Error(data.message));
      });
    });
  };
}));

All this does is wrap each function so that I can use the new async/await syntax, e.g.:

import client from "./client";

async function getMarkets() {
  const markets = await client.getmarkets();
  console.log("markets:", markets);  // [{ BaseCurrency: "BTC", MarketCurrency: "BTC", ... }, ...]
}

The problem is now I'm trying to call the endpoint multiple times in a promise loop:

const tickers = ["BTC-NEO", "BTC-ETH", /* ... */];

await Promise.all(tickers.map(async (ticker) => {
  const candles = await client.getcandles({ marketName: ticker, tickInterval: "oneMin" });
  await this.saveCandles(candles);
}));

Although the loop works properly, and I see that getcandles is called with iterating marketName values (i.e.: "BTC-NEO", then "BTC-ETH", etc.), the debug output shows that params never change for each "concurrent" request:

requested from https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-LTC&tickInterval=oneMin in: 0.539s requested from https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-LTC&tickInterval=oneMin in: 0.539s requested from https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-LTC&tickInterval=oneMin in: 0.539s requested from https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-LTC&tickInterval=oneMin in: 0.539s

I'm not sure, but I think the problem is that within the node.bittrex.api package, request_options is shared across all calls. I assume the package was not intended to be used in this way, but it would be convenient if it could be since I otherwise have to wait for one request has to complete before moving onto the next. I'm trying to maximize speed and make my app efficient.

mhuggins commented 7 years ago

Thanks for merging my fix! 😄

n0mad01 commented 7 years ago

thanks for the pull request - i also added you to the contributors list :)

mhuggins commented 7 years ago

Awesome, thanks!