0

Actually, this question can be simplified to "Return an $http or Restangular call result in a promise". My goal is to have a promise object that is resolved after an $http call completes:

var deferredCall= $q.defer();

Then this is resolved from the call:

$http.get (url)
   .then(function(result){ deferredCall.resolve('Success' + result);},
    function(){ deferredCall.resolve('Failure' + error););

Then I have a promise object that will be resolved when the call is complete (either succeeding or failing), this is what I want:

deferredCall.promise.then(function (value) {
    return //something to pass to controller;
});

The goal is that the controller is instantiated whether the resolve succeds or fails. My problem? Resolve can only take a promise, so: `deferredCall.promise. How do I resolve this promise with the call above withing the Resolve / through a service? Are the results of a service method a promise?

Like if I make a service whose method makes the $http call and then returns deferredCall?

||||||
  • why do u need $q for this? – Poyraz Yilmaz Sep 10 '15 at 12:47
  • I have no idea how else to do it while using angularjs. That's not me saying "There is no other way to do it" - I just really don't know how to approach this problem in javascript. $q was my guess. I am looking for whatever will achieve the end goal of having a promise that returns the results of an $http call whether it fails or succeeds. The call DOES need to be nested within another promise because if a resolve call fails, the controller isn't instantiated. – VSO Sep 10 '15 at 12:50
  • 1
    @VSO you don't need to Create another promise..just return the $http.get promise.. – Pankaj Parkar Sep 10 '15 at 12:52
  • @VSO You don't use to $q both Restangular and $http will give you promise already... – Poyraz Yilmaz Sep 10 '15 at 12:52
  • @PankajParkar: That's what I am doing now, but it in some resolves it still fails if the controller throws an error. I am probably doing something else wrong. Will look at it now. Thanks. – VSO Sep 10 '15 at 12:55
3

This is how we resolve data in our project:

Angular $routeProvider:

$routeProvider
    .when('/something/something', {
        templateUrl: 'somewhere/some-details.html',
        controller : SomeController,
        resolve : {
            someItem : function (SomeService) {
                return SomeService.getSomethingAll();
            }
        }
    })

Controller:

var SomeController = function ($scope, someItem) {};

Data Service:

.service('SomeService', function (SomeUtils, $http) {
    return {
        getSomethingAll : function () {
            return SomeUtils.promiseHttpResult($http.get("api/something/all"));
        }
    }
})

Utils Service:

.service("SomeUtils", function($q) {
    return {
        promiseHttpResult: function (httpPromise) {
            var deferred = $q.defer();
            httpPromise.success(function (data) {
                deferred.resolve(data);
            }).error(function () {
                deferred.reject(arguments);
            });
            return deferred.promise;
        }
    }
})

Nice and simple. No skills required :) (DD)

||||||
  • Thanks. That's what I needed. – VSO Sep 10 '15 at 17:21
  • Are you sure the last example works? In my case, deferred.promise is just accepted immediatly without waiting. Even if I comment resolve & reject out, it wents through – Simon Fakir Aug 16 '16 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.