0
$scope.pagination_data = function(page_data){
   count = page_data.data.count;
};

console.log("outside count is",count);// this not working

var page = function(){
 userService.paginate()
  .then(function(user){
    $scope.pagination_data(user);
    console.log("count is",count);//this is works
  });
};

in that code i m using $http service by using that i m getting data aftr that i want a count then i m getting count also but now i want this count variable access in controller but this is not accessible to me. what should i do..?

4
  • You're logging the value before you call the function. Commented Mar 27, 2015 at 10:15
  • either declare count outside of the function, or just return count; the use it like that Commented Mar 27, 2015 at 10:17
  • @yoavmatchulsky Returning a value from an asynchonous function is useless. Commented Mar 27, 2015 at 10:18
  • oh, I thought he only wanted the value inside the promise. my bad Commented Mar 27, 2015 at 10:24

3 Answers 3

1

Actually, both works but you're calling the first one at the wrong time.

The .then() indicates that paginate() returns a promise. Which should be a hint that it is asynchronous.

To prove this, delay calling your first console.log by using setTimeout:

$scope.pagination_data = function(page_data){
   count = page_data.data.count;
};

setTimeout(function(){
    console.log("outside count is",count);// this should work
}, 5000); // wait 5 seconds before calling the code above
// If 5 seconds is not long enough increase it to 10 or something

var page = function(){
 userService.paginate()
  .then(function(user){
    $scope.pagination_data(user);
    console.log("count is",count);//this is works
  });
};

What is asynchronous code? Asynchronous simply means that the code will be called later. What paginate().then() does is not call the function(user) function immediately but rather remembers that it is supposed to call function(user) (which then calls your pagination_data function to set the value of count) at a later time. It then continues on running other stuff.

When there's no other code to run, the event loop gets processed and whatever userService.paginate() needs to do asynchronously gets processed.

When whatever userService.paginate() is waiting for finally returns (this may be a network request, user click etc) the function(user) function finally gets called. This in turn calls $scope.pagination_data() which is what assigns the result to the global count variable.

What does that setTimeout() do? Well, it does the same thing I describe above. Instead of calling console.log() immediately it remembers to call it later. Then when 5 seconds (5000 milliseconds) expires, and there is no other javascript code running (this is important because it means javascript can run the event loop), the console.log() finally gets called.

Sign up to request clarification or add additional context in comments.

2 Comments

thnks dear..bt could i used that count variable directly with other operations like i want to calculate page size so var page_size = count / limit ; is this fine
Put all those other code inside the function(user) function like you did your second console.log(). That's the only way to do it. It's the nature of asynchronous programming
0

Just define the variable outside.

var count = 0;
$scope.pagination_data = function(page_data){
   count = page_data.data.count;
};

console.log("outside count is",count);// this not working

var page = function(){
 userService.paginate()
  .then(function(user){
    $scope.pagination_data(user);
    console.log("count is",count);//this is works
  });
};

3 Comments

i m defining varable outside also bt not getting count
check what values you get inside the pagination_data function by logging them out. I guess you never initialize them
first i am initialize count variable to zero.
0

The fist statement only defines the function, it doesn't call it. If you have data to pass to that function call it before logging the count.

   // this only defines the function, doesn't call it
    $scope.pagination_data = function(page_data){
       count = page_data.data.count;
    };

    console.log("outside count is",count);// count is still unset

    var page = function(){
     userService.paginate()
      .then(function(user){
        $scope.pagination_data(user); // this is the call, count will be set
        console.log("count is",count);//this will work
      });
    };

2 Comments

k..bt now i m changing the sequence also still not getting count
var page = function(){ userService.paginate() .then(function(user){ $scope.pagination_data(user); console.log("count is",count); }); }; $scope.pagination_data = function(page_data){ count = page_data.data.count; }; console.log("outside count is",count);

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.