简体   繁体   中英

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'
     }
   }

 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM