0

I am trying to call a function and use the value from that function to the global variable. The problem is value is not going outside. I am kinda not so good in javascript. I don't know if i wrote the function correctly.

async function leader(platformName){

let fullUrl = getUrlPrefix(platformName) + "createsessionjson/" + devKey + "/" + sig('createsession') + "/" + timestamp;


        const response = await fetch(fullUrl, { method: 'GET' });
        const json = await response.json();
        console.log(json);
        console.log(json.session_id);
        return (json.session_id);



}
var session1 = ''; 

export async function leaderboard (platformName) {

    var session =  await leader(platformName);
    console.log("Session" + ":" + " " + session);
    session1 = session;
}

export function leaderboardsession (platformName) {

    console.log("Global session" + ":" + " " + session1);
    let fullUrl1 =  getUrlPrefix(platformName) + "getleagueleaderboardjson/" + devKey + "/" + sig('getleagueleaderboard') + "/" + session1 + "/" + timestamp + "/" + 428 + "/" + 27 + "/" + 2;

    console.log("this is inner url ->" + "\n" + fullUrl1);
    return (fetch(fullUrl1, { method: 'GET' }))

        .then(function (response) {
            return response.json();

        });

}

Output i am getting

Here the global session is empty. I want to call leaderboard() one time, store the session in global session variable and use the global session in leaderboardsession() [as many time i call leaderboardsession()].

How do i store that ?

Cinah
  • 15
  • 6
  • how and where and when are you calling the leaderboard function? The example is incomplete – ADyson Nov 01 '18 at 15:34
  • `return session` from leaderboard, then do `var session1 = leaderboard();` and finally `await session1` whenever you want to use it – Jonas Wilms Nov 01 '18 at 15:35
  • @JonasWilms for that, leaderboard() would have to return a value, surely? – ADyson Nov 01 '18 at 15:36
  • But how do i store the session to session1 ? bit confused – Cinah Nov 01 '18 at 15:46
  • @Cinah you don't seem to ever be calling the leaderboard() function. It won't populate if you don't call the function. See my first comment – ADyson Nov 01 '18 at 15:47
  • Thanks @Jonas Wilms . I am getting the global session1 value. I have a small question. Is it actually storing the session value in session1 when doing "var session1 = leaderboard();" OR i am calling session1 "await session1" everytime whenever i want to use, which in turn will call leaderboard(); Could u plz clarify this ? – Cinah Nov 01 '18 at 16:12

1 Answers1

2
 var session1;

 async function leaderboard() {
   // something async that is finisjed *somewhen*
   session1 = session;
 }

 leaderboard(); // I assume you planned to call it

The code above is bad as it assumes that leaderboard() finishes before you use session1 elsewhere and that is quite error prone as leaderboard() might take longer as you assume it would, and then session1 stays undefined and causes trouble as the functions relying on it fail.

Therefore some small rules that you should always (/mostly/sometimes) follow to prevent sich things:

1) Don't use global variables. 2) And never mutate them from inside an async function.

Instead return the results from the async function. Calling the function will return a Promise, so if you do:

 async function leaderboard() {
   //...
   return session;
 }

 var session1 = leaderboard();

you immeadiately start executing leaderboard, and that will be executec only once (as you just call it once), but session1 contains a promise, so you always know if leaderboard() is already done. If you do

 await session1

inside another async function, that async function will stop there until the execution of leaderboard was done. That way, if leaderboard() wasn't done yet, all functions relying on it will wait, if it finished execution already awaiting it will proceed immeadiately.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151