How to make angular wait for response of DWR Call?

204 views Asked by At
function with DWR Call  
self.getUsers = function()
        {
            console.log("Get User called");
             userProjectDWRManager.getAllUsers(function(list){
            console.log(list);
           return list;
        });
      }


  var list = self.getUsers();
console.log(list) //undefined

I tried promise concept but I don't understand exactly how to apply it with DWR exactly.

Edit:

The whole scenario is that I am in

 $transitions.onStart({
      }, function(trans) {
// get list here from DWR call
    if(list.length==0)
                {
                    event.preventDefaults();
                    $state.go('register',{});
                }

      }

In this function I want to get the list and wait for the list to return before going to the next state. But it always goes to the next state.

1

There are 1 answers

10
Azad On BEST ANSWER

you can pass a reference to a callback function into promise function to execute after promise resolved.

self.getUsers = function(callback)
        {
            console.log("Get User called");
             userProjectDWRManager.getAllUsers(function(list){
             callback(list);//executes the callback function
        });
      }


self.getUsers(showList); //pass reference to callbak function

function showList(list){
  console.log(list);
}

for your updated question

        $transitions.onStart({
    }, function(trans) {
        // get list here from DWR call
        userProjectDWRManager.getAllUsers(function(list){
            if(list.length==0)
            {
                event.preventDefaults();
                $state.go('register',{});
            }


            else
                console.log('list is Not empty');
        });
    });