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

Closes: #13574
Closes: #13545
This commit is contained in:
thorn0
2015-12-18 00:27:32 +02:00
committed by Lucas Mirelmann
parent 04efdd5bfa
commit b3ef5e0852
2 changed files with 13 additions and 5 deletions
+4 -5
View File
@@ -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;
+9
View File
@@ -221,6 +221,15 @@ 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);
/*jshint newcap: true */
});
describe('resolve', function() {
it('should fulfill the promise and execute all success callbacks in the registration order',