0

I'm new to javascript and I find this very surprising as I come from python. All modules I've seen so far cover how their functions will work with console.log(output), but modifying the code to return output won't work at all as it's returning undefined. As an example, I'm including two such modules:

https://www.npmjs.com/package/cryptocompare
https://www.npmjs.com/package/node-coinmarketcap

As you can see, cryptocompare gives an example on the usage of their function:

cc.priceMulti('BTC', 'USD')
.then(prices => {
  console.log(prices)
  // -> { BTC: { USD: 1114.63 } }
})

Changing this code to:

cc.priceMulti('BTC', 'USD')
.then(prices => prices )

will now return undefined, whereas it was logging correct object before. I tried googling and searching for documentations for this, but I don't know a correct query that would describe this question. I found tons of modules that just support console.logging, but never indicate how do we make the objects tangible. I wonder why is this happening, because after all the reason we all want to use modules is to use their returned objects in one way or another, and not only print them right?
This is what returns undefined: const cc = require('cryptocompare')

function fetchPrices(){
cc.priceMulti(['BTC', 'ETH', 'LTC', 'DASH'], ['USD'])
.then(prices => prices)
}
console.log(fetchPrices())
Συλχαν
  • 45
  • 1
  • 6
  • `prices => { prices }` is a function that doesn't return anything. What do you expect? – SLaks Apr 30 '18 at 21:11
  • I tried both prices => prices and prices => {prices}. I'll edit my post. Isn't prices => prices supposed to be returning some value? – Συλχαν Apr 30 '18 at 21:13
  • How do you know that your second example *returns* undefined? How do you check that? I'm asking since the promise based approach of `then` is asynchronous. – E. Sundin Apr 30 '18 at 21:14
  • I'll include answer to that in the post. – Συλχαν Apr 30 '18 at 21:16
  • 2
    [`.then(prices => prices)` is pointless](https://stackoverflow.com/q/41089122/1048572), it just returns the same promise. Drop it. And learn how to use promises. – Bergi Apr 30 '18 at 21:16
  • "*I found tons of modules that just support console.logging, but never indicate how do we make the objects tangible.*" - the console.log is just an example that you should replace by your own code. Which can use the `prices` inside that callback. – Bergi Apr 30 '18 at 21:18
  • 2
    You're not returning anything from the `fetchPrices()` function in your example. You might want to read up on promises. – E. Sundin Apr 30 '18 at 21:20
  • It's like you are seeing documentation that gives the example `console.log(3 * 5)`, and you say that the multiplication operator only allows logging. – Bergi Apr 30 '18 at 21:20
  • Except that return 3 * 5 would return 15 instead of undefined – Συλχαν Apr 30 '18 at 21:22
  • Anyway can anyone answer how to write a code that would enable returning an object or point me to a right link? I searched resolving promises but I don't directly see their application here – Συλχαν Apr 30 '18 at 21:24
  • https://blog.slaks.net/2015-01-04/async-method-patterns/ – SLaks Apr 30 '18 at 21:24
  • You just need to use a return statement. You're currently not returning anything from fetchPrices. Just do this: `function fetchPrices(){ return cc.priceMulti(['BTC', 'ETH', 'LTC', 'DASH'], ['USD']); }`. Then `fetchPrices()` correctly returns a Promise, so the caller can do things like `fetchPrices().then( prices => { console.log( prices ); } )` or `const prices = await fetchPrices(); console.log( prices );`. – Paul Apr 30 '18 at 21:29
  • I just tried adapting the code from that link and the output is always Promise pending – Συλχαν Apr 30 '18 at 21:29
  • `.then(prices => prices)` doesn't do anything so you can leave that off. – Paul Apr 30 '18 at 21:31
  • Related: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Paul Apr 30 '18 at 21:35
  • You could do `.then(prices => { console.log(prices); return prices; })`. – David Knipe Apr 30 '18 at 21:49

1 Answers1

0

Try:

function fetchPrices(){
  return cc.priceMulti(['BTC', 'ETH', 'LTC', 'DASH'], ['USD'])
}

async function main () {
  const result = await fetchPrices();
  console.log(result);
  // do things with result
}
main().catch(error => console.error(error));
generalhenry
  • 17,227
  • 4
  • 48
  • 63