How to return a deferred .resolve () function from function to function

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 also:

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

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

so I need to defer the myFunc solution, which is called in another func, which is called in my controller .. and displays it there

EDIT I returned the deferred forecast. BUT it returns ONLY the first promise of the SERVICE function, not myFunc, and I need the promise myFunc

EDIT 2

Look also at Carson Drake's answer, this is not an anti-pattern!

+4
source share
2 answers

In fact, you can reduce two by one chained code 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

+4
source

You need to return defer.promise from the factory service (whatever you use)

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;
}
0
source

All Articles