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

Commit b3ef5e0

Browse files
thorn0lgalfaso
authored andcommitted
fix($q): make instanceof work for $q promises
Closes: #13574 Closes: #13545
1 parent 04efdd5 commit b3ef5e0

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/ng/q.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,6 @@ function qFactory(nextTick, exceptionHandler) {
566566
throw $qMinErr('norslvr', "Expected resolverFn, got '{0}'", resolver);
567567
}
568568

569-
if (!(this instanceof Q)) {
570-
// More useful when $Q is the Promise itself.
571-
return new Q(resolver);
572-
}
573-
574569
var deferred = new Deferred();
575570

576571
function resolveFn(value) {
@@ -586,6 +581,10 @@ function qFactory(nextTick, exceptionHandler) {
586581
return deferred.promise;
587582
};
588583

584+
// Let's make the instanceof operator work for promises, so that
585+
// `new $q(fn) instanceof $q` would evaluate to true.
586+
$Q.prototype = Promise.prototype;
587+
589588
$Q.defer = defer;
590589
$Q.reject = reject;
591590
$Q.when = when;

test/ng/qSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ describe('q', function() {
221221
expect(typeof promise.finally).toBe('function');
222222
});
223223

224+
it('should support the instanceof operator', function() {
225+
/*jshint newcap: false */
226+
var promise = new q(noop);
227+
expect(promise instanceof q).toBe(true);
228+
promise = q(noop);
229+
expect(promise instanceof q).toBe(true);
230+
/*jshint newcap: true */
231+
});
232+
224233

225234
describe('resolve', function() {
226235
it('should fulfill the promise and execute all success callbacks in the registration order',

0 commit comments

Comments
 (0)