diff options
| author | Brian Ford | 2013-08-21 16:40:20 -0700 |
|---|---|---|
| committer | Brian Ford | 2013-08-21 16:58:40 -0700 |
| commit | 7d188d630c63fde05d8765d0ad2d75a5baa8e5d3 (patch) | |
| tree | 8889de135b6f156e21a2d4bdb900d5351fd96bb2 | |
| parent | 8ee9a3e902e523577f9ea331af56c7bad6b4eea9 (diff) | |
| download | angular.js-7d188d630c63fde05d8765d0ad2d75a5baa8e5d3.tar.bz2 | |
fix($q): fix forwarding resolution when callbacks aren't functions
Uses the changes from @jamestalmage's fix in #3535. (thanks!)
Closes #3535
| -rw-r--r-- | src/ng/q.js | 18 | ||||
| -rw-r--r-- | test/ng/qSpec.js | 32 |
2 files changed, 41 insertions, 9 deletions
diff --git a/src/ng/q.js b/src/ng/q.js index 307dc1b4..cc39967c 100644 --- a/src/ng/q.js +++ b/src/ng/q.js @@ -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); @@ -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); @@ -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); } @@ -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) { @@ -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(); @@ -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; } @@ -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); @@ -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); @@ -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); } diff --git a/test/ng/qSpec.js b/test/ng/qSpec.js index bad67dcc..13c51f4b 100644 --- a/test/ng/qSpec.js +++ b/test/ng/qSpec.js @@ -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!)']); + }); }); |
