-1

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:

  1. Accepts a parameter (trading pair).
  2. Returns the data for that trading pair in the form of a Promise.
  3. Then calls another function which processes that data.

Function for pulling trading details

// 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.

Using the function in another file

// 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.

Oliver Chalk
  • 176
  • 4
  • 19

2 Answers2

1

I'm not sure what it is you like pullData to return. you would like it to return only the data? So something like this:

function pullData(coinPair) {
  return kc.getTicker({
    pair: coinPair
  })
  .catch(console.error)  //.then(returnData)
}

And then you'd like it to receive a calculations function that is performed on the data like this:

function pullData(coinPair,calculations) {
  return kc.getTicker({
    pair: coinPair
  }).then(calculations).catch(console.error)  
}

Now the caller can do:

pullData(pair,calculations);

But the caller may ass well do:

pullData(par).then(calculations)

I'm not sure what the benefit here is.

HMR
  • 37,593
  • 24
  • 91
  • 160
  • I would like to export the function. And when I call the exported function I want it to return the data and accept a callback. – Oliver Chalk Dec 10 '17 at 02:36
  • 2
    Then you would need to `return kc.getTicker(...)` inside `pullData` so the Promise is still available to run your callback on - like `pullData(abc).then(yourCallback)` – Deryck Dec 10 '17 at 03:07
  • @Deryck I completely missed that `pullData` wasn't returning. Used syntax like `fn=>doSomething().then(` instead of `funciton{return...}` too much. – HMR Dec 10 '17 at 04:58
  • @OliverChalk See comment from `Deryck`, I updated the answer, all you need to do is have `pullData` return the promise. – HMR Dec 10 '17 at 05:00
  • Okay so using your answer, I know have my functions return their promises. This alone did not entirely solve my issues, but with a little searching, I was able to push the promises into an array. Now I can access all the data once it resolves. Thank you so much for your help! – Oliver Chalk Dec 10 '17 at 06:09
  • 1
    @OliverChalk Good to know you solved it. Here is an answer explaining a little about why promises are used, how to convert callback code to promises and `Promise.all` that won't fail if one promise fails: https://stackoverflow.com/a/47678417/1641941 The video link in there is very good I think, explains a lot of why JS uses promises and where your resolve/reject handlers go (on the queue). – HMR Dec 10 '17 at 06:37
0

My question may have confused others, as I was genuinely lost as to the proper structure to follow.

Upon review the following day, what I was trying to do was:

  1. Create a function that would request data from an API.
  2. Export this function and allow it to be called in other files.
  3. Allow those other files to pass in their own callbacks in order to perform their desired calculations and use the data appropriately.

In my original question, I did not define a callback as a parameter for the data collecting function. As a result, the function would only ever expect one parameter, namely the 'pair'. I believe it would then call '.then()' once the asynchronous process was finished. However, the function it calls would have to be within the original file.

To remedy this I had to add a function, in this case, named 'calculations', as a parameter in the function and then have '.then()' call that function. This told '.then()' to look for the function that would be passed as the second parameter and then call it once the data had been received from the API.

This now means I can export the function to multiple other files and if I want other functions to use the data I just have to pass those functions in with the call.

Oliver Chalk
  • 176
  • 4
  • 19