未在函数 onReady 中获取全局变量的更新值

Not getting updated value of global variable inside function onReady

我在 api 执行函数之外获取变量的更新值时遇到问题。我在这里使用 JIVE,所以我使用 'osapi' get 方法。

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

 

使用 Promise Resolve 来解决这个问题。

参考link:在promise和resolve中使用函数

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