0

My variable has no value after calling the service. I defined the success function as a separate function and called it in the .then() method, here's the code:

// Code inside the Ctrl:
var successData = null;
vm.fillVar = null;
vm.loadData = loadData;

// service func
function loadData() {
   return $timeout(function () {
      vm.fillVar = vm.fillVar || [];

      if (vm.fillVar == undefined || vm.fillVar.length == 0) {
          CrudSvc.GetAllData().$promise.then(success, error);

          console.log('Success Variable: ', successData); //--> here, the successData variable contains no data
          vm.fillVar = successData;
          console.log('fillVar for the view: ', vm.fillVar); //--> fillVar is empty, why??
      }
   }, 500);
}

// success func
function success(res) {
   console.log('var res: ', res); //--> res contains data
   successData = res;
   console.log('success func: ', successData); //--> the variable contains data
}

I don't understand why the successData variable is empty after calling the success function. It is a bit confusing... Has anyone a solution for this issue?

5
  • can u post the code for CrudSvc.GetAllData() Commented Apr 6, 2017 at 8:39
  • 2
    Why don't you just set vm.fillVar inside function success and avoid using successData variable Commented Apr 6, 2017 at 8:39
  • 1
    you are expecting synchronous execution out of asynchronous call. Hope you know the core JS Commented Apr 6, 2017 at 8:42
  • @sand Because I want to use the success-method also for other services in the same controller and it is better to read. Commented Apr 6, 2017 at 8:48
  • @yuro check my answer, it should work for you Commented Apr 6, 2017 at 8:49

2 Answers 2

3

This code is async CrudSvc.GetAllData().$promise.then(success, error); so if you want to execute something after that you should change your code to:

CrudSvc.GetAllData().$promise.then(success, error).then(function(){
     // this code is executed after all the previous code is done and succeed
     console.log('Success Variable: ', successData); 
     vm.fillVar = successData;
     console.log('fillVar for the view: ', vm.fillVar); 
});

You can read about then chaining HERE

The then method returns a Promise which allows for method chaining.

EDIT

If you want to handle error situation simply use reject function like this:

CrudSvc.GetAllData().$promise.then(success, error).then(function(){
     // this code is executed if previous success 
}, function(){
     // this code is executed if previous error
}); // This is how you do further error handling via this approach
Sign up to request clarification or add additional context in comments.

3 Comments

What is, when an error is expected? I defined the err variables in error function, which will show the error in the view immediately.
thanks for your support. Currently is this a good approach :)
You are welcome, I'm glad that this code helped you :)
0
// Code inside the Ctrl:
var successData = null;
vm.fillVar = null;
vm.loadData = loadData;

// service func
function loadData() {
   return $timeout(function () {
      vm.fillVar = vm.fillVar || [];

      if (vm.fillVar == undefined || vm.fillVar.length == 0) {
          CrudSvc.GetAllData().$promise.then(function(response) {
successData = response;
    console.log('Success Variable: ', successData); //--> here, the success variable now contains no data
          vm.fillVar = successData;
          console.log('fillVar for the view: ', vm.fillVar); //--> fillVar is empty, why??
}, function(error) {
});


      }
   }, 500);
}

1 Comment

That solution I already know. But not the expected solution I am looking for. However thanks for your support :)

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.