Asynchronously fetching Bitcoin orders and quotes I have the following code to get data using async/await

import fetch from 'node-fetch';

let bid = 0;
let ask = 0;
let lastBuy = 0;
let buyOrders = [];
let sellOrders = [];
const currency = process.argv[3].toUpperCase();

const getOrderBook = async () =&gt; {
  const orderBook = await fetch(`https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-${currency}&amp;type=both`)
    .then(response =&gt; response.json())
    .then(response =&gt; response.result)
    .then(result =&gt; ({ buys: result.buy.slice(0, 10), sells: result.sell.slice(0, 10) }));

  return orderBook;
};

const getTicker = async () =&gt; {
  const ticker = await fetch(`https://bittrex.com/api/v1.1/public/getticker?market=BTC-${currency}`)
    .then(response =&gt; response.json())
    .then(response =&gt; response.result);

  return ticker;
};

const getCurrencyData = () =&gt; {
  getOrderBook().then(orderBook =&gt; ([buyOrders, sellOrders] =  [orderBook.buys, orderBook.sells] )); //eslint-disable-line
  getTicker().then(ticker =&gt; ([bid, ask, lastBuy] = [ticker.Bid, ticker.Ask, ticker.Last])); //eslint-disable-line
};


setInterval(() =&gt; {
  getCurrencyData();
}, 5000);


This is the first time I use async/await, so I have the following doubts:

First, on my getOrderBook and getTicker function I can't find a way to return the values without first assigning to a variable.

Also, it looks very ugly in the getCurrencyData function, because after I called the functions I have to make a ".then" to assign the data to my variables.
One of the main benefits of using await is to keep your code flat and to avoid .thens - you're not doing that in getOrderBook. nor in getTicker. If you want to use await, try awaiting each asynchronous operation.

Accessing a property of an object is not an asynchronous operation, nor is creating a new object: those lines should not be in .thens.

Because both fetch() and .json() access the .result property, you can keep from repeating yourself by putting that into another function:

const fetchBittrexResult = async (method) =&gt; {
  const params = 'market=BTC-' + currency + (method === 'getorderbook' ? '&amp;type=both' : '');
  const url = `https://bittrex.com/api/v1.1/public/${method}?${params}`;
  const response = await fetch(url);
  const responseObj = await response.json();
  return responseObj.result;
};


Then consume it:

const getOrderBook = async () =&gt; {
  const { buy, sell } = await fetchBittrexResult('getorderbook');
  return {
    buys: buy.slice(0, 10),
    sells: sell.slice(0, 10),
  };
};

const getTicker = () =&gt; fetchBittrexResult('getticker');


(no need for getTicker to be async anymore)

For getCurrencyData, I'm doubtful that continually re-assigning top-level variables is a good way to do this, but without seeing the consumers of the fetched data, it's hard to say what the better solution is. Argument destructuring will help reduce the syntax noise there, and don't feel like you have to squash everything onto one long line:

const getCurrencyData = () =&gt; {
  getOrderBook()
    .then(({ buys, sells }) =&gt; ([buyOrders, sellOrders] =  [buys, sells] ));
  getTicker()
    .then(({ Bid, Ask, Last }) =&gt; ([bid, ask, lastBuy] = [Bid, Ask, Last]));
};


It would probably be better to assign to an object rather than to multiple standalone variables, though.