0

Hope all is well. Using node w fetch & trying to 'Get' the contents of a JSON file to be used in other functions in my script & possibly other scripts. Until now, i can only use them inside the fetch scope which is not optimal. pretty new to this but been workin on it for a couple days, read/watched a ton of tutorials, & searched here as well w no luck:-( for instance i'd like to do something like:

fetch (json){ }

let thePrice = data.price;

let theAmntSold = data.price;

....then later in script be able to use data.price & theAmntSold ie

totalAmntEarned(thePrice, theAmntSold) {
return thePrice * theAmntSold;
}

here is the latest I came up with and no luck

let theSystem, themaxPrice,
theblockPrices = [],
mainObj = {};

let showObj = function() {
//asyncronous so we need to set up for loop in a function
for (let prop in mainObj) {
    console.log(prop);
    console.log(mainObj[prop]);
}

};


fetch(URL, {
    method: 'GET',
    headers: {
        Authorization: AUTH_KEY
    }

}).then(function(resp) {
    return resp.json();
}) //assign variables below
.then(function(data) {
    showObj();
    let theblockPrices = data.blockPrices;
    
    console.log(data.maxPrice);
    console.log(theblockPrices[0]);
    });

so basically, i'd like to be able to use 'theblockPrices' in another function or variable. Also, when i call the showObj() inside the fetch block, I get no output in the console. Any tips would be amazing!

thanks

  • welcome to async code. Thats just how it works. You gotta wait until its there, either by callback, .then or await. – The Fool Sep 07 '21 at 20:37
  • That would require time travel. By the time the data is available, your "outer scope" has already finished what it needed to do and returned. Just use the promise instead, .then can be called repeatedly on a given promise to get the same data out of it – Kevin B Sep 07 '21 at 20:38
  • Cmon donnie lol, why'd you close this? I was writing an answer. – Dan Zuzevich Sep 07 '21 at 20:40
  • 1
    @DanZuzevich there's a few dozen existing questions revolving around this that you could apply your answer to (such as one dupe target) – Kevin B Sep 07 '21 at 20:40
  • 1
    @DanZuzevich, Donnie didn't close this, and you shouldn't be writing an answer. – The Fool Sep 07 '21 at 20:41
  • ok. I'll check out some of ur references. would be cool to open it up for like an hour or so no? as I said, i tried so many of the examples i found here on stackoverflow & nothing was working. thanks – donnie b Sep 07 '21 at 20:49
  • @TheFool thanks, did a few variations of async no luck. also, i used the .then in this one. appreciate the reply – donnie b Sep 07 '21 at 20:54
  • @KevinB - thanks. will try again. Isn't the .then supposed to take care of that tho? – donnie b Sep 07 '21 at 20:55
  • `then` is called when the data is available and lets you do stuff with it *then* (hence the name). It can't be used to send the data back in time so it was available when you previously called another function while waiting for the asynchronous code to resolve. – Quentin Sep 07 '21 at 21:00
  • @Quentin - ok. makes sense. thanks for the help. will give it a shot – donnie b Sep 07 '21 at 21:07

0 Answers0