0

UPDATE...RESOLVED: see note below

I'm trying to retrieve a crypto price from https://min-api.cryptocompare.com/data/price?fsym=HIVE&tsyms=USD then use it in a formula as a variable. Sounds simple and likely is, but I've tried several tutorials with no luck.

A working live version of the code is at http://crrdlx.websavvy.work/hivetiptokens/ or at https://codepen.io/crrdlx/pen/wvqNBWZ The problematic code is below. Note that const HIVEusd = 2; the "2" is just a dummy for testing purposes. HIVEusd should be equal to the "USD" value retrieved from the API.

Thank you for any help and your patience. :)

<script language=javascript>
const toFix = str => (+str).toFixed(6); // perhaps should do 6 to show more precision?
const calc = $div => {
  
// Retrieve HIVE price in USD
  $.getJSON("https://min-api.cryptocompare.com/data/price?fsym=HIVE&tsyms=USD", function(data) {
    $("#HIVEPrice").text(toFix(data["USD"]));
    calc($("#HIVEPrice"));
  }).fail(function(dat, textStatus, error) {
    var err = textStatus + ", " + error;
    alert(err);
  });
// End HIVE price retrieve
  
// Begin calculations for table of live values
  const $row = $div.closest("tr")
  const $cells = $row.find("td");
  const qty = +$cells.eq(2).text().split(" ")[0];
  const val = $div.text()
  // console.log($div.attr("id"),val, qty, val * qty)
  const res = val * qty
  $cells.eq(3).text(isNaN(res) ? "" : res.toFixed(4)); // or (2) but then several will show 0.00
  
  // figure the USD value: res (HIVE value) times HIVEusd equals token USD value
  const HIVEusd = 2;
  const valusd = res * HIVEusd;
  $cells.eq(4).text(isNaN(valusd) ? "" : valusd.toFixed(4)); // or (4) for more precision in decimals
};
// End table price calculations
crrdlx
  • 21
  • 1
  • 7
  • 2
    Move your calculations to callback of `getJSON`. Now you `// Begin calculations` even before your `$("#HIVEPrice")` is set – Justinas Nov 23 '21 at 12:07
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – CBroe Nov 23 '21 at 12:12
  • Thanks @Justinas . I tried this, but no luck yet. What I really can't figure out is, what should `const HIVEusd =` be set to (instead of "2")? Updated the codepen at https://codepen.io/crrdlx/pen/wvqNBWZ – crrdlx Nov 24 '21 at 12:04
  • That's a great explanation and helps me understand things. I'll have to play around with it. My gut tells me my code is not an async issue. I say that since I'm able to use the json data (HIVEPrice) and display it in html. I think I'm unable to correctly format or write it was a variable to be used. Could be wrong though! @CBroe – crrdlx Nov 24 '21 at 12:19
  • 2
    @crrdlx _"My gut tells me my code is not an async issue"_ - `$.getJSON` _is_ async, and therefor you need to work with the results inside of the callback function. (It looks like you kinda tried that already, with `calc($("#HIVEPrice"))` - but that would call the same function you are already in, again, and execute the getJSON again as well.) – CBroe Nov 24 '21 at 12:24
  • RESOLVED: My gut was correct...I could not declare the variable correctly, until I could. Thank you to those who helped me! The solution was as simple as adding the line `const HIVEusd = document.getElementById("HIVEPrice").innerHTML;` Corrected code at https://codepen.io/crrdlx/pen/wvqNBWZ and working live version at http://crrdlx.websavvy.work/hivetiptokens/ – crrdlx Nov 25 '21 at 14:49

0 Answers0