I wrote this little code in a custom service in AngularJS.
In my service:
var deferred = $q.defer();
var promise = deferred.promise;
If you want to change the default behavior of something that is injected by angular, you can use the decorator() method on the $provide service.
var myApp = angular.module("myApp", []);
myApp.config(function ($provide) {
$provide.decorator("$q", function($delegate) {
// The $delegate argument here refers to the $q service.
$delegate.defer = function() {
alert("You just tried to call defer()!");
};
// Now, every time angular provides an instance of $q via
// injection, it will return your customized version of $q.
return $delegate;
});
});
See the example above in action at http://plnkr.co/edit/RuZF2cGkVHwlu7NIhxEZ?p=preview
As to modifying $q to add the success and error functions, I am not sure at the moment. But I am pretty sure that this is where you'd want to do it.