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

Commit a207665

Browse files
bolasblackIgorMinar
authored andcommitted
feat($q): add shorthand for defining promise error handlers
Now we can instead this promise.then(null, errorHandler) with this promise.catch(errorhandler) Closes #2048 Closes #3476
1 parent b308742 commit a207665

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/ng/q.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
* This method *returns a new promise* which is resolved or rejected via the return value of the
9292
* `successCallback` or `errorCallback`.
9393
*
94+
* - `catch(errorCallback)` – shorthand for `promise.then(null, errorCallback)`
95+
*
9496
* - `always(callback)` – allows you to observe either the fulfillment or rejection of a promise,
9597
* but to do so without modifying the final value. This is useful to release resources or do some
9698
* clean-up that needs to be done whether the promise was rejected or resolved. See the [full
@@ -128,25 +130,25 @@
128130
* you can treat promises attached to a scope as if they were the resulting values.
129131
* - Q has many more features than $q, but that comes at a cost of bytes. $q is tiny, but contains
130132
* all the important functionality needed for common async tasks.
131-
*
133+
*
132134
* # Testing
133-
*
135+
*
134136
* <pre>
135137
* it('should simulate promise', inject(function($q, $rootScope) {
136138
* var deferred = $q.defer();
137139
* var promise = deferred.promise;
138140
* var resolvedValue;
139-
*
141+
*
140142
* promise.then(function(value) { resolvedValue = value; });
141143
* expect(resolvedValue).toBeUndefined();
142-
*
144+
*
143145
* // Simulate resolving of promise
144146
* deferred.resolve(123);
145147
* // Note that the 'then' function does not get called synchronously.
146148
* // This is because we want the promise API to always be async, whether or not
147149
* // it got called synchronously or asynchronously.
148150
* expect(resolvedValue).toBeUndefined();
149-
*
151+
*
150152
* // Propagate promise resolution to 'then' functions using $apply().
151153
* $rootScope.$apply();
152154
* expect(resolvedValue).toEqual(123);
@@ -267,8 +269,13 @@ function qFactory(nextTick, exceptionHandler) {
267269

268270
return result.promise;
269271
},
272+
273+
"catch": function(callback) {
274+
return this.then(null, callback);
275+
},
276+
270277
always: function(callback) {
271-
278+
272279
function makePromise(value, resolved) {
273280
var result = defer();
274281
if (resolved) {
@@ -278,14 +285,14 @@ function qFactory(nextTick, exceptionHandler) {
278285
}
279286
return result.promise;
280287
}
281-
288+
282289
function handleCallback(value, isResolved) {
283-
var callbackOutput = null;
290+
var callbackOutput = null;
284291
try {
285292
callbackOutput = (callback ||defaultCallback)();
286293
} catch(e) {
287294
return makePromise(e, false);
288-
}
295+
}
289296
if (callbackOutput && callbackOutput.then) {
290297
return callbackOutput.then(function() {
291298
return makePromise(value, isResolved);
@@ -296,7 +303,7 @@ function qFactory(nextTick, exceptionHandler) {
296303
return makePromise(value, isResolved);
297304
}
298305
}
299-
306+
300307
return this.then(function(value) {
301308
return handleCallback(value, true);
302309
}, function(error) {

test/ng/qSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ describe('q', function() {
516516
expect(typeof promise.then).toBe('function');
517517
});
518518

519+
it('should have a catch method', function() {
520+
expect(typeof promise['catch']).toBe('function');
521+
});
522+
519523
it('should have a always method', function() {
520524
expect(typeof promise.always).toBe('function');
521525
});
@@ -881,6 +885,14 @@ describe('q', function() {
881885

882886
});
883887
});
888+
889+
describe('catch', function() {
890+
it('should be a shorthand for defining promise error handlers', function() {
891+
promise['catch'](error(1)).then(null, error(2))
892+
syncReject(deferred, 'foo');
893+
expect(logStr()).toBe('error1(foo)->reject(foo); error2(foo)->reject(foo)');
894+
});
895+
});
884896
});
885897
});
886898

0 commit comments

Comments
 (0)