1

Problem: I want to make a variable inside a function as a global variable

This works:

var x;
function myFunction() {
    x = 999;
}
myFunction();
console.log(x);

But this one doesn't work while trying to declare a global variable from API Result

webOS.service.request(url, {
    onSuccess: function (data) {
        var serial = data.idList[0].idValue;
        var udid = serial; // This is the variable that I want
        callback(udid); // Trying to get this udid out of this API call
},
});

var sn;
function callback(udid) {
    sn = udid; // I want this as my global variable
}
callback(udid); // produces an error which says udid not defined
console.log(sn); // undefined

How can I make var sn as global variable? Thanks in advance

  • It works. But you are accessing the variable before it was assigned value. – Felix Kling Apr 06 '19 at 05:57
  • See also my post: [JavaScript: How (not) to get a value “out of” a callback](https://felix-kling.de/blog/2019/javascript-callbacks-misconceptions.html) – Felix Kling Apr 06 '19 at 05:59
  • so the problem is with this asynchronous nature of API request? –  Apr 06 '19 at 06:00
  • Yep. Why do you want a global variable? You most likely don't need it. You can refactor almost any code into functions and pass dependencies as arguments. – Felix Kling Apr 06 '19 at 06:01
  • @FelixKling thank you so much for your article, reading at it right now. Really appreciate it –  Apr 06 '19 at 06:03
  • @FelixKling I need the UDID (LG Smart TV Unique Device iD) as a global variable so that I can assign that variable into my another function. My another function is a login function which post to API: username, password, udid –  Apr 06 '19 at 06:06
  • *"so that I can assign that variable into my another function"* You don't need a global variable for that. You just have to pass the ID as argument to the function. Simplified example: `request(..., function(response) { login(response.uuid); }); function login(uuid) { request({username: foo, password: bar, uuid: uuid}) }`. That's the whole point of functions having parameters. – Felix Kling Apr 06 '19 at 06:09
  • @FelixKling the login function post to another different API. The summary like this: Function 1 GetUDID: I get from API 1, the UDID API. Function 2 Login: I post to API 2 the Login API (on different server), using the parameter username, password and udid which I get from function 1 –  Apr 06 '19 at 06:20
  • @FelixKling But I think I somewhat get the glimpse of what you just suggest it. I will try your suggestion to, as to combine those 2 function. Which is I put function 2 inside function 1 correct. So the scheme was like this, one function -> on success make a function getudid -> inside the function getudid -> i create another function which post to login API. Kinda somewhat like that right? –  Apr 06 '19 at 06:23
  • That works too, but you don't have to *create* the login function inside the other function. You just have to *call* it. Take this example: `function foo(x) { console.log(x); }; function bar(y) { foo(y); }; bar(42);` `foo` doesn't have to be *defined* inside `bar` to access `42`. `bar` simply calls `foo` and passes the value to it. In the same way, *call* the login function on success, passing the uuid to it. – Felix Kling Apr 06 '19 at 07:35

1 Answers1

0

It's because udid is not defined in the scope you're calling callback in - you're calling callback in your onSuccess function, so you don't need to call it again. You also need to place your console.log inside your callback function:

webOS.service.request(url, {
    onSuccess: function (data) {
        var serial = data.idList[0].idValue;
        var udid = serial; // This is the variable that I want
        callback(udid); // Trying to get this udid out of this API call
    },
});
var sn;
function callback(udid) {
    sn = udid; // I want this as my global variable
    console.log(sn);
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79