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

fix($q): make instanceof work for $q promises #13574

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions test/ng/qSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down