Skip to content

Commit

Permalink
fix($q): make $q.reject support finally and catch
Browse files Browse the repository at this point in the history
Add support for the functions `finally` and `catch` to the
promise returned by `$q.reject`

Closes angular#6048
  • Loading branch information
lgalfaso committed Jan 31, 2014
1 parent 5ed721b commit eefdd57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function qFactory(nextTick, exceptionHandler) {


reject: function(reason) {
deferred.resolve(reject(reason));
deferred.resolve(_reject(reason));
},


Expand Down Expand Up @@ -380,6 +380,12 @@ function qFactory(nextTick, exceptionHandler) {
* @returns {Promise} Returns a promise that was already resolved as rejected with the `reason`.
*/
var reject = function(reason) {
var result = defer();
result.reject(reason);
return result.promise;
};

var _reject = function(reason) {
return {
then: function(callback, errback) {
var result = defer();
Expand Down
7 changes: 7 additions & 0 deletions test/ng/qSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,13 @@ describe('q', function() {
mockNextTick.flush();
expect(log).toEqual(['errorBroken(rejected)->throw(catch me!)', 'errorAffected(catch me!)->reject(catch me!)']);
});


it('should have functions `finally` and `catch`', function() {
var rejectedPromise = q.reject('rejected');
expect(rejectedPromise['finally']).not.toBeUndefined();
expect(rejectedPromise['catch']).not.toBeUndefined();
});
});


Expand Down

0 comments on commit eefdd57

Please sign in to comment.