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

fix($q): fix forwarding resolution when callbacks aren't functions #3694

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
18 changes: 9 additions & 9 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function qFactory(nextTick, exceptionHandler) {

var wrappedCallback = function(value) {
try {
result.resolve((callback || defaultCallback)(value));
result.resolve((isFunction(callback) ? callback : defaultCallback)(value));
} catch(e) {
result.reject(e);
exceptionHandler(e);
Expand All @@ -250,7 +250,7 @@ function qFactory(nextTick, exceptionHandler) {

var wrappedErrback = function(reason) {
try {
result.resolve((errback || defaultErrback)(reason));
result.resolve((isFunction(errback) ? errback : defaultErrback)(reason));
} catch(e) {
result.reject(e);
exceptionHandler(e);
Expand All @@ -259,7 +259,7 @@ function qFactory(nextTick, exceptionHandler) {

var wrappedProgressback = function(progress) {
try {
result.notify((progressback || defaultCallback)(progress));
result.notify((isFunction(progressback) ? progressback : defaultCallback)(progress));
} catch(e) {
exceptionHandler(e);
}
Expand Down Expand Up @@ -297,7 +297,7 @@ function qFactory(nextTick, exceptionHandler) {
} catch(e) {
return makePromise(e, false);
}
if (callbackOutput && callbackOutput.then) {
if (callbackOutput && isFunction(callbackOutput.then)) {
return callbackOutput.then(function() {
return makePromise(value, isResolved);
}, function(error) {
Expand All @@ -322,7 +322,7 @@ function qFactory(nextTick, exceptionHandler) {


var ref = function(value) {
if (value && value.then) return value;
if (value && isFunction(value.then)) return value;
return {
then: function(callback) {
var result = defer();
Expand Down Expand Up @@ -375,7 +375,7 @@ function qFactory(nextTick, exceptionHandler) {
then: function(callback, errback) {
var result = defer();
nextTick(function() {
result.resolve((errback || defaultErrback)(reason));
result.resolve((isFunction(errback) ? errback : defaultErrback)(reason));
});
return result.promise;
}
Expand All @@ -401,7 +401,7 @@ function qFactory(nextTick, exceptionHandler) {

var wrappedCallback = function(value) {
try {
return (callback || defaultCallback)(value);
return (isFunction(callback) ? callback : defaultCallback)(value);
} catch (e) {
exceptionHandler(e);
return reject(e);
Expand All @@ -410,7 +410,7 @@ function qFactory(nextTick, exceptionHandler) {

var wrappedErrback = function(reason) {
try {
return (errback || defaultErrback)(reason);
return (isFunction(errback) ? errback : defaultErrback)(reason);
} catch (e) {
exceptionHandler(e);
return reject(e);
Expand All @@ -419,7 +419,7 @@ function qFactory(nextTick, exceptionHandler) {

var wrappedProgressback = function(progress) {
try {
return (progressback || defaultCallback)(progress);
return (isFunction(progressback) ? progressback : defaultCallback)(progress);
} catch (e) {
exceptionHandler(e);
}
Expand Down
32 changes: 32 additions & 0 deletions test/ng/qSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,38 @@ describe('q', function() {
mockNextTick.flush();
expect(log).toEqual(['error(oops!)->reject(oops!)']);
});

it('should forward success resolution when success callbacks are not functions', function() {
deferred.resolve('yay!');

promise.then(1).
then(null).
then({}).
then('gah!').
then([]).
then(success());

expect(logStr()).toBe('');

mockNextTick.flush();
expect(log).toEqual(['success(yay!)->yay!']);
});

it('should forward error resolution when error callbacks are not functions', function() {
deferred.reject('oops!');

promise.then(null, 1).
then(null, null).
then(null, {}).
then(null, 'gah!').
then(null, []).
then(null, error());

expect(logStr()).toBe('');

mockNextTick.flush();
expect(log).toEqual(['error(oops!)->reject(oops!)']);
});
});


Expand Down