4

How do i get deferre.resolve from a function?

my Controller calls this:

service.getData().then(function(response){ //myFunc deferred response});

in Service:

var _getData = function (){
  $http.get(url).success(function(response){
   deferred.resolve(_myFunc(response.Message)); // or just myFunc doesnt matter
  });

  return deferred.promise; //it returns in the end of the function
}

and myFunc has also:

$http.get(url).success(function(response){
 deferred.resolve(response); 
});

return deferred.promise; // also my Func is returning

so i need the deferred resolve of myFunc, which is called in another func which is called in my controller.. and display it there

EDIT I have return deferred.promise BUT it returns ONLY the first promise of the SERVICE function not myFunc, and i need the promise of myFunc

EDIT 2

Look also at Carson Drake's answer, it isn't anti-pattern!

2 Answers 2

4

You can actually reduce the two into one chained call if you want to simplify if.

var _getData = function(){
return $http.get(url).then(function(response1){
    return $http.get(response1.data);
}).then(function(response2){
    return response2;
});

UPDATE Plunkr

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

1 Comment

Indeed, there is no use in wrapping promises around... promises as $http already returns a promise. See this post for more information.
0

You need to return defer.promise from the service of factory(whatever you are using)

var _getData = function (){
 var deferred = $q.defer();
 $http.get(url).success(function(response){
   deferred.resolve(_myFunc(response.Message)); // or just myFunc doesnt matter
});
return deferred.promise;
}

1 Comment

trying @squiroid solutions.. like i said i have return deffered i edited.. :3

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.