From 8bee7f5ffe5f804dbbe9f6a1a2201459e7de7a77 Mon Sep 17 00:00:00 2001 From: thorn0 Date: Fri, 18 Dec 2015 00:27:32 +0200 Subject: [PATCH] fix($q): make instanceof work for $q promises --- src/ng/q.js | 9 ++++----- test/ng/qSpec.js | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ng/q.js b/src/ng/q.js index f5716520f062..8656670efd63 100644 --- a/src/ng/q.js +++ b/src/ng/q.js @@ -566,11 +566,6 @@ function qFactory(nextTick, exceptionHandler) { throw $qMinErr('norslvr', "Expected resolverFn, got '{0}'", resolver); } - if (!(this instanceof Q)) { - // More useful when $Q is the Promise itself. - return new Q(resolver); - } - var deferred = new Deferred(); function resolveFn(value) { @@ -586,6 +581,10 @@ function qFactory(nextTick, exceptionHandler) { return deferred.promise; }; + // Let's make the instanceof operator work for promises, so that + // `new $q(fn) instanceof $q` would evaluate to true. + $Q.prototype = Promise.prototype; + $Q.defer = defer; $Q.reject = reject; $Q.when = when; diff --git a/test/ng/qSpec.js b/test/ng/qSpec.js index 824728528823..0ff0b1f18ee1 100644 --- a/test/ng/qSpec.js +++ b/test/ng/qSpec.js @@ -221,6 +221,14 @@ describe('q', function() { expect(typeof promise.finally).toBe('function'); }); + it('should support the instanceof operator', function() { + /*jshint newcap: false */ + var promise = new q(noop); + expect(promise instanceof q).toBe(true); + promise = q(noop); + expect(promise instanceof q).toBe(true); + }); + describe('resolve', function() { it('should fulfill the promise and execute all success callbacks in the registration order',