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);
3
  • 2
    The issue is that getInfo is asynchronus, this is a duplicate of How to return value from an asynchronous callback function?. Commented Dec 2, 2014 at 2:54
  • This is another asynchronous js question. Try throwing some console.log statements in and see what happens Commented Dec 2, 2014 at 2:54
  • It logs the data that I want to store into a variable. Commented Dec 2, 2014 at 2:59

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

I now get an empty array.
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.
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.
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.
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;
};

2 Comments

I don't know how but I still receive an undefined result.
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.