javascriptjqueryapijive

Not getting updated value of global variable inside function onReady


I am facing an issue to get the updated value of the variable outside the api execution function. Here I am working with JIVE so I am using 'osapi' get method.

function onReady(tileConfig, tileOptions, viewer, container) {
   var connectLanguage = ""

   osapi.jive.core.get({
        v: "v3",
        href: "/people/@me"
    }).execute(function (response) {
        try {
            connectLanguage = response.jive.locale;
        }
        catch(error) {
            connectLanguage = "";
            console.log(error)
        }
    })
     console.log(connectLanguage) //getting 'undefined' as a result.

     if(connectLanguage === "en") {
        //execute function which depends on 'connectLanguage'
     }else {
       //execute function which depends on other value of 'connectLanguage'
     }
   }

 

Solution

  • Use Promise Resolve to solve this problem.

    reference link: Use function in promise and resolve

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve