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 () => {
const orderBook = await fetch(`https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-${currency}&type=both`)
.then(response => response.json())
.then(response => response.result)
.then(result => ({ buys: result.buy.slice(0, 10), sells: result.sell.slice(0, 10) }));
return orderBook;
};
const getTicker = async () => {
const ticker = await fetch(`https://bittrex.com/api/v1.1/public/getticker?market=BTC-${currency}`)
.then(response => response.json())
.then(response => response.result);
return ticker;
};
const getCurrencyData = () => {
getOrderBook().then(orderBook => ([buyOrders, sellOrders] = [orderBook.buys, orderBook.sells] )); //eslint-disable-line
getTicker().then(ticker => ([bid, ask, lastBuy] = [ticker.Bid, ticker.Ask, ticker.Last])); //eslint-disable-line
};
setInterval(() => {
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.