Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

@egobrain egobrain commented Jan 27, 2014

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

@wejendorp wejendorp commented May 20, 2014

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

@lawrencejones lawrencejones commented Jul 15, 2014

@wejendorp very nice, thanks for that!

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

@lgalfaso lgalfaso commented Jan 3, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants
You can’t perform that action at this time.