I am trying to export a function that will let you put in a parameter (trading pair) and it will return the data for that trading pair. This is the code that creates a function that:
- Accepts a parameter (trading pair).
- Returns the data for that trading pair in the form of a Promise.
- Then calls another function which processes that data.
// This function is exported and let's you feed in pairs you want data for - feeling proud.
function pullData(coinPair) {
kc.getTicker({
pair: coinPair
}).then(returnData).catch(console.error)
}
// This function is the callback, which I believe means this function is called and fed the results of the Promise returned from 'kc.getTicker'.
// At first it just logs the data, but now I'll need to find a way to return the data as a usable array. Maybe use the 'return()' function?
function returnData(pairData) {
// Nae Nae
//return pairData;
console.log(pairData);
}
// Finally we export our hard work
exports.pullData = pullData;
I now want to simply have the exported function return the data (and for this data to be usable by a callback function.
// Including important sub modules & config files
const tradeData = require('./example');
var config = require('./config');
// Log the config pairs (Method 1):
function logArrayElements(element, array) {
console.log(element);
}
config.pairs.forEach(logArrayElements);
// Was going to cycle through config pairs. However, I will instead have a base strategy that will be replicated for each pair and run side by side.
//console.log(tradeData.pullData('ETH-USDT'));
tradeData.pullData('ETH-USDT', calculations);
function calculations() {
console.log(pairData);
}
The relevant lines here are the including of the file ('./example') and the pitiful attempt at using the function with a callback down lower.
My goal is to be able to pass something like this:
tradeData.pullData('ETH-USDT', calculations);
function calculations() {
// Calculations and logic here
}
That may involve having '.then' simply return the data. I believe this would allow for a 'calculations' function to use the data after the asynchronous function finishes...
Any answers are greatly appreciated as I haven't been able to find guidance as to what I should do here.