Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Decoration of $q defer.promise #5998

Closed
egobrain opened this issue Jan 27, 2014 · 3 comments
Closed

Decoration of $q defer.promise #5998

egobrain opened this issue Jan 27, 2014 · 3 comments
Milestone

Comments

@egobrain
Copy link

I want to decorate $q defer promise. Here is some code:

 $provide.decorator('$q', function ($delegate) {
    var defer = $delegate.defer;
    $delegate.defer = function () {
      var deferred = defer();
      deferred.promise.success = function (fn) {
        deferred.promise.then(function (value) {
          fn(value);
        });
        return deferred.promise;
      };
      deferred.promise.error = function (fn) {
        deferred.promise.then(null, function (value) {
          fn(value);
        });
        return deferred.promise;
      };
      return deferred;
    };
    return $delegate;

All is ok in simple case:

var def = $q.defer()
def.promise.success(function(data) {...});

But when i use $q functions like then, all, when returned promise has no success and error methods, because of internally $q closures old defer function implementation and uses non-decorated code for constructing defer objects.

@wejendorp
Copy link

I made a post about this issue and a way around it on my blog:
Extending $q promises

Check it out if it's still relevant.

@lawrencejones
Copy link

@wejendorp very nice, thanks for that!

@btford btford removed the gh: issue label Aug 20, 2014
@lgalfaso
Copy link
Contributor

lgalfaso commented Jan 3, 2015

With the post from @wejendorp, I think this can be closed

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants