AngularJS Angular promises with $q service Avoid the $q Deferred Anti-Pattern

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Avoid this Anti-Pattern

var myDeferred = $q.defer();

$http(config).then(function(res) {  
   myDeferred.resolve(res);
}, function(error) {
   myDeferred.reject(error);
});

return myDeferred.promise;

There is no need to manufacture a promise with $q.defer as the $http service already returns a promise.

//INSTEAD
return $http(config);

Simply return the promise created by the $http service.



Got any AngularJS Question?