全局变量的奇怪问题

时间:2018-04-11 22:48:08

标签: javascript global-variables

我有一个全局变量,url2没有被获取函数更新

这是我的JS代码:

var url2 = ""; //Here I define url2, as a global variable
fetch("https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1").then(
    function response(response) {
        return response.json();
    }).then(
    function printOut(myJSON) {
        identification = myJSON["deck_id"];
        url2 = (("https://deckofcardsapi.com/api/deck/" + identification + "/draw/?count=1").toString()); //Here, I change the global variable
        console.log("URL2 in fetch block",url2); //If I print out variable url2, here, it returns the correct response
    }
)
console.log("URL2 after fetch block",url2); //If I print out variable url2 here, however, it returns "" -- like it was previously defined to.

我对JS很新,我不确定为什么会这样。

0 个答案:

没有答案