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

fix($q): make $q.reject support finally and catch #6076

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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one thing I don't like about this, is the naming of this function... I think it's a bit confusing, and would be better renamed to something like rejectImpl or something. I'm not sure everyone else would share this same opinion, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. we should call it something like createInternalRejectedPromise or something of that sort. we already have 3 other reject methods/fields in this file so 4th is making it even worse especially since it's an internal-only factory function.

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