0

How can I set the var response in the socket.onmessage function. The var response is undefinded but I had set in the function. But I need the response variable because of the callback at the end of my function.

function getVariables_Groups(_callback) {
            var self = this;
            var response = {};

            socket.onmessage = function(evt) {
                json = JSON.parse(JSON.parse(evt.data));

                var statistics = {};
                var groups = json.args;
                statistics.action = "internal_dashboard_statistics";

                if(groups[0] == "notfound") {
                    statistics.args[0] = "notfound";

                    loadVariable("dashboard.htm", statistics);
                } else {
                    statistics.args = [];

                    for(var i = 0; i < json.args.length; i++) {
                        statistics.args[i] = {};
                        statistics.args[i].name = groups[i];
                    }

                    console.log(statistics);
                    self.response = statistics;
                }
            }
            console.log(response);

            _callback(response);

        }
Nightloewe
  • 606
  • 1
  • 9
  • 21

1 Answers1

1

You can't use a variable before it has been set.

You need to only call the callback once you actually have a value.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896