userMod2
userMod2

Reputation: 8960

Angular - Do i need to use promises for each $http call?

I'm using $http to make various gets and posts to my API however I want to ask should i use a 'deferred/promise' approach or is a simple return ok?

What's the best practice/most stable way?

So - code form:

var deferred = $q.defer();
            var theReq = {
             method: 'POST',
             url: API + '/class',
             data: {
               option: option
             }
};

$http(theReq)
            .then(function(data){
              deferred.resolve(data);
})

return deferred.promise;

Versus:

return $http.post(API + '/class', {
      option: option
})

Upvotes: 1

Views: 51

Answers (1)

kartsims
kartsims

Reputation: 1008

Angular does the Promise abstraction for you. Why write 20 lines of code when you only need 3 and let Angular deal with the deferring process ?

Go for the second solution

Upvotes: 5

Related Questions