Skip to content

Instantly share code, notes, and snippets.

@demetriusnunes
Last active August 29, 2015 14:04
Show Gist options
  • Save demetriusnunes/4ef1783dbf8036c60620 to your computer and use it in GitHub Desktop.
Save demetriusnunes/4ef1783dbf8036c60620 to your computer and use it in GitHub Desktop.
Extending $q promises with a .set method
angular.module("myApp")
.config(function ($provide) {
$provide.decorator('$q', function($delegate) {
var defer = $delegate.defer;
$delegate.defer = function() {
var deferred = defer();
deferred.promise.set = function(object, property) {
return deferred.promise.then(function(value) {
object[property] = value;
return value;
});
};
return deferred;
};
return $delegate;
});
});
angular.module("myApp").controller(function ($scope, myServiceApi) {
// traditional way
myServiceApi.getData().then(function (data) {
$scope.data = data;
});
// simpler way
myServiceApi.getData().set($scope, "data");
});
@willmendesneto
Copy link

Pretty cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment