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

Commit 7d188d6

Browse files
committed
fix($q): fix forwarding resolution when callbacks aren't functions
Uses the changes from @jamestalmage's fix in #3535. (thanks!) Closes #3535
1 parent 8ee9a3e commit 7d188d6

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/ng/q.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ function qFactory(nextTick, exceptionHandler) {
241241

242242
var wrappedCallback = function(value) {
243243
try {
244-
result.resolve((callback || defaultCallback)(value));
244+
result.resolve((isFunction(callback) ? callback : defaultCallback)(value));
245245
} catch(e) {
246246
result.reject(e);
247247
exceptionHandler(e);
@@ -250,7 +250,7 @@ function qFactory(nextTick, exceptionHandler) {
250250

251251
var wrappedErrback = function(reason) {
252252
try {
253-
result.resolve((errback || defaultErrback)(reason));
253+
result.resolve((isFunction(errback) ? errback : defaultErrback)(reason));
254254
} catch(e) {
255255
result.reject(e);
256256
exceptionHandler(e);
@@ -259,7 +259,7 @@ function qFactory(nextTick, exceptionHandler) {
259259

260260
var wrappedProgressback = function(progress) {
261261
try {
262-
result.notify((progressback || defaultCallback)(progress));
262+
result.notify((isFunction(progressback) ? progressback : defaultCallback)(progress));
263263
} catch(e) {
264264
exceptionHandler(e);
265265
}
@@ -297,7 +297,7 @@ function qFactory(nextTick, exceptionHandler) {
297297
} catch(e) {
298298
return makePromise(e, false);
299299
}
300-
if (callbackOutput && callbackOutput.then) {
300+
if (callbackOutput && isFunction(callbackOutput.then)) {
301301
return callbackOutput.then(function() {
302302
return makePromise(value, isResolved);
303303
}, function(error) {
@@ -322,7 +322,7 @@ function qFactory(nextTick, exceptionHandler) {
322322

323323

324324
var ref = function(value) {
325-
if (value && value.then) return value;
325+
if (value && isFunction(value.then)) return value;
326326
return {
327327
then: function(callback) {
328328
var result = defer();
@@ -375,7 +375,7 @@ function qFactory(nextTick, exceptionHandler) {
375375
then: function(callback, errback) {
376376
var result = defer();
377377
nextTick(function() {
378-
result.resolve((errback || defaultErrback)(reason));
378+
result.resolve((isFunction(errback) ? errback : defaultErrback)(reason));
379379
});
380380
return result.promise;
381381
}
@@ -401,7 +401,7 @@ function qFactory(nextTick, exceptionHandler) {
401401

402402
var wrappedCallback = function(value) {
403403
try {
404-
return (callback || defaultCallback)(value);
404+
return (isFunction(callback) ? callback : defaultCallback)(value);
405405
} catch (e) {
406406
exceptionHandler(e);
407407
return reject(e);
@@ -410,7 +410,7 @@ function qFactory(nextTick, exceptionHandler) {
410410

411411
var wrappedErrback = function(reason) {
412412
try {
413-
return (errback || defaultErrback)(reason);
413+
return (isFunction(errback) ? errback : defaultErrback)(reason);
414414
} catch (e) {
415415
exceptionHandler(e);
416416
return reject(e);
@@ -419,7 +419,7 @@ function qFactory(nextTick, exceptionHandler) {
419419

420420
var wrappedProgressback = function(progress) {
421421
try {
422-
return (progressback || defaultCallback)(progress);
422+
return (isFunction(progressback) ? progressback : defaultCallback)(progress);
423423
} catch (e) {
424424
exceptionHandler(e);
425425
}

test/ng/qSpec.js

+32
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,38 @@ describe('q', function() {
730730
mockNextTick.flush();
731731
expect(log).toEqual(['error(oops!)->reject(oops!)']);
732732
});
733+
734+
it('should forward success resolution when success callbacks are not functions', function() {
735+
deferred.resolve('yay!');
736+
737+
promise.then(1).
738+
then(null).
739+
then({}).
740+
then('gah!').
741+
then([]).
742+
then(success());
743+
744+
expect(logStr()).toBe('');
745+
746+
mockNextTick.flush();
747+
expect(log).toEqual(['success(yay!)->yay!']);
748+
});
749+
750+
it('should forward error resolution when error callbacks are not functions', function() {
751+
deferred.reject('oops!');
752+
753+
promise.then(null, 1).
754+
then(null, null).
755+
then(null, {}).
756+
then(null, 'gah!').
757+
then(null, []).
758+
then(null, error());
759+
760+
expect(logStr()).toBe('');
761+
762+
mockNextTick.flush();
763+
expect(log).toEqual(['error(oops!)->reject(oops!)']);
764+
});
733765
});
734766

735767

0 commit comments

Comments
 (0)