#Polling live data
Polling live data
###Some points first.
Some points first.
###Ignoring the promise
Ignoring the promise
##Getters make life easy.
Getters make life easy.
#Polling live data
###Some points first.
###Ignoring the promise
##Getters make life easy.
Polling live data
Some points first.
Ignoring the promise
Getters make life easy.
By the looks of it you are not pushing the data forward after you fetch it, which means you dontdon't need to know when the fetch cycle is complete. This allows for a few short cuts-cuts.
- Move the urlsURLs to the top as constants.
- Separate out the API address
- Collect the data
bid
,ask
,last
, ... etc in a single object. - Use a single
async
function and pass the URL and data destination to it. - Encapsulate your state and expose only the states required,
As you don't seam to be doing anything with the fetched data, you can ignore the Promise
returned by the async function
.
By the looks of it you are not pushing the data forward after you fetch it which means you dont need to know when the fetch cycle is complete. This allows for a few short cuts.
- Move the urls to the top as constants.
- Separate out the API address
- Collect the data
bid
,ask
,last
, ... etc in a single object. - Use a single
async
function and pass the URL and data destination to it. - Encapsulate your state and expose only the states required,
As you don't seam to be doing anything with the fetched data you can ignore the Promise
returned by the async function
.
By the looks of it you are not pushing the data forward after you fetch it, which means you don't need to know when the fetch cycle is complete. This allows for a few short-cuts.
- Move the URLs to the top as constants.
- Separate out the API address
- Collect the data
bid
,ask
,last
, ... etc in a single object. - Use a single
async
function and pass the URL and data destination to it. - Encapsulate your state and expose only the states required,
As you don't seam to be doing anything with the fetched data, you can ignore the Promise
returned by the async function
.
#Polling live data
I am taking the code at face value. If you are pushing data forward then this answer does not apply.
By the looks of it you are not pushing the data forward after you fetch it which means you dont need to know when the fetch cycle is complete. This allows for a few short cuts.
###Some points first.
- Move the urls to the top as constants.
- Separate out the API address
- Collect the data
bid
,ask
,last
, ... etc in a single object. - Use a single
async
function and pass the URL and data destination to it. - Encapsulate your state and expose only the states required,
###Ignoring the promise
As you don't seam to be doing anything with the fetched data you can ignore the Promise
returned by the async function
.
An async
function returns a Promise
which can not be avoided, but you can side step the promise and assign the resulting object inside the function.
const bitcoinFeed = (() => {
const stream = {ticker: {}, orderBook: {}}
const interval = 5000;
const currency = process.argv[3].toUpperCase();
const API = "https://bittrex.com/api/v1.1/public/";
const orderURL = `getorderbook?market=BTC-${currency}&type=both`;
const tickerURL = `getticker?market=BTC-${currency}`;
// The function assigns the resulting object to data so you can ignore
// the returned promise.
async function getData(url, data) {
Object.assign(data, (await (await fetch(API + url)).json()).result);
}
// fetch data every 5 sec
(function updateData() {
getData(tickerURL, stream.ticker);
getData(orderURL , stream.orderBook);
setTimeout(updateData, interval);
})()
// return the object containing live data
return stream;
})();
##Getters make life easy.
With a few modifications you can return an object containing getters
const bitcoinFeed = (() => {
const ticker = {};
const orderBook = {};
const interval = 5000;
const currency = process.argv[3].toUpperCase();
const API = "https://bittrex.com/api/v1.1/public/";
const orderURL = `getorderbook?market=BTC-${currency}&type=both`;
const tickerURL = `getticker?market=BTC-${currency}`;
const getData = async (url, data) =>
Object.assign(data, (await (await fetch(API + url)).json()).result);
;(function updateData() {
getData(tickerURL, ticker);
getData(orderURL , orderBook);
setTimeout(updateData, interval);
})()
// return the object containing live data
return {
get bid() { return ticker.Bid },
get ask() { return ticker.Ask },
get last() { return ticker.Last },
get sells() { return orderBook.sells.slice(0, 10) },
get buys() { return orderBook.buys.slice(0, 10) },
};
})();
All the data wrapped in a neat object and not a then
in sight.