Passing Variables from one Function to Another

In function 1- I have a function that get a Json Results from a third party source. The json result I have got as a array. From the array I have created different variables. The variables I can echo and I get the wanted results.

Now I want to use the $ (variables from Function 1 in Function 2) - I have tried passing them but for me it seems that they are “unset” in after function 1 is ran. (as It should be?)

I would like to invoke function 1 and pass the variables created from the array in function 2.

I search the forum and found this post (passing an array/data from one function to another) but it wasnt replied so hope I can get a feedback on how to doing this.

JSON is asynchronous meaning the data traffic goes both ways, so when you try to take data from one function to another functions you need to do a callback function which can be tricky. However, I would look into using fetch (or $.get in JQuery I believe) or looking into callback functions which once you get the hang of is really not that hard.

here’s a small example of some code in what I did for a vanilla JavaScript game that I have develop

   /* retrieve User Data from hs_table */
const retrieveHSTableUISuccess = function (info) {
    displayHSTable(info);

};

/* If Database Table fails to save data in mysql table */
const retrieveHSTableUIError = function (error) {
    console.log("Database Table did not load", error);
};

/* Create High Score Data using fetch */
const createHSTable = (retrieveUrl, succeed, fail) => {
    var max = 5; // Maximum Records to Be Displayed
    var maxium = {};
    maxium.max_limit = max;

    fetch(retrieveUrl, {
        method: 'POST', // or 'PUT'
        body: JSON.stringify(maxium)
    })
            .then((response) => handleErrors(response))
            .then((data) => succeed(data))
            .catch((error) => fail(error));
}; 

at the very top is the call to the displayHSTable(info) and that is the most import thing to remember as the data (info in this case) never leaves a function (callback function). Now I like using fetch ($.get) as I don’t have to worry about parsing the JSON data as it is already parsed. However, doing it the other way with straight AJAX call isn’t that much harder, but forgetting to parse the data coming in can be a pain as you think it’s a problem with the way you are bring back the data. Anyways, it’s frustrating and it took me a long time to understand it, but what I finally did was watched some tutorials online. :rofl:

I hope that helps ~ John

Oh, I forgot mention you might have to have an API key if you are bringing in data from a third party. That can also trip you up.

Returning data from one function call and supplying it as the input to another function call, which you seem to indicate you tried, is how you would solve this. Without knowing what your code attempt was, we have nothing to specifically help you with. Your attempted code would indicate both the context/meaning/amount of the data and what your (mis)understanding of the process is.

Sponsor our Newsletter | Privacy Policy | Terms of Service