0

Is there a way to store data to a variable?

I tried:

$scope.another =
        function(){
            var new_data;
            userService.getInfo().success(function(data){
               new_data = data;
            });
            return new_data;
        };

var data = $scope.another();

but it returns 'undefined' in the console log. Thank you

EDIT

I now get an empty array for new_data .

var new_data = [];

$scope.another = 
                function(callback){
                    userService.getInfo().success(function(data){
                        paymentService.getCashierParams({ "cardNumber": data.cardNumber}).success(function(data){
                            gameService.getAllgames({ "PID":data.GetCashierParameters.PID, "limit": 6, "skinID": 1}).success(function(data) {
                                callback(data.data.GetFlashGamesResult.Data.FlashGame);
                            });
                        });
                    });
                };
          $scope.another(function(result){
                    new_data = result;
                });
                console.log(new_data);
sanosuke
  • 19
  • 5
  • 2
    The issue is that `getInfo` is asynchronus, this is a duplicate of [How to return value from an asynchronous callback function?](http://stackoverflow.com/q/6847697/359284). – Kevin Brown-Silva Dec 02 '14 at 02:54
  • This is another asynchronous js question. Try throwing some console.log statements in and see what happens – Stephen Dec 02 '14 at 02:54
  • It logs the data that I want to store into a variable. – sanosuke Dec 02 '14 at 02:59

2 Answers2

1

You need to think about this problem differently. Your getInfo method returns a promise. A promise's success callback is never immediately called. It may be called sometime in the future, but in the meantime your code will continue executing and the return value of $scope.another will be undefined.

Instead, place whatever logic you wish to execute within the success callback.

userService.getInfo().success(function (data) {
    // Do stuff with data.
});

If you are not used to working with asynchronous data, this may seem weird. But it is much better than the alternative, which is hanging the page for potentially many seconds while a network request, database read, etc, completes.

If you are worried about indentation, you can create separate function(s) to handle the data.

function processData(data) {
    // Process stuff...
    return data;
}

function handleData(data) {
    data = processData(data);
    console.log(data);
}

userService.getInfo().success(handleData);
Jackson
  • 9,188
  • 6
  • 52
  • 77
  • I now get an empty array. – sanosuke Dec 02 '14 at 06:46
  • In your latest edit, you are still not handling your logic in your callback. `$scope.another(function (result) { console.log(result); });` would work. The callback passed to `$scope.another` is not going to execute before the `console.log(new_data);` executes. Also, you are "getting" an empty array because you set `new_data` to an empty array on line 1. You don't even need the `new_data` variable, just get rid of it. – Jackson Dec 02 '14 at 07:33
  • What am I supposed to do so that `console.log(new_data)` executes after `$scope.another`? I noticed that `console.log` was executed before `$scope.another`. – sanosuke Dec 02 '14 at 08:07
  • 1
    It's impossible. That's not how promises work. Do not try to solve the problem with sequential lines of code. Instead, solve the problem with a series of functions that pass data to each other. – Jackson Dec 02 '14 at 08:32
0

This is due to the asynchronous function that you called. Try to use callback instead. Something like this:

$scope.another =
    function(fn){
        userService.getInfo().success(function(data){
           fn(data);
        });

    };


  var data = $scope.another(function(doSomething) {
    alert(doSomething);
    return doSomething;
};
geckob
  • 7,680
  • 5
  • 30
  • 39
  • I don't know how but I still receive an undefined result. – sanosuke Dec 02 '14 at 06:39
  • @sanosuke: Where did you put the console.log? If you do it after $scope.another call, it makes sense. Because async will not wait but instead go to the next line. Try declare var data something else first then call $scope.another. If the data still have the old value, means the async call is not finish yet. Use promise – geckob Dec 02 '14 at 19:54