Skip to content

Commit 880b6e1

Browse files
committed
feat($q): add shorthand for defining promise error handlers
Now we can instead this promise.then(null, errorHandler) with this promise.catch(errorhandler) Closes angular#2048
1 parent 285f6b4 commit 880b6e1

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/ng/q.js

+9-5
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)` – it is 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
@@ -230,6 +232,9 @@ function qFactory(nextTick, exceptionHandler) {
230232

231233

232234
promise: {
235+
"catch": function(callback) {
236+
return this.then(null, callback);
237+
},
233238
then: function(callback, errback, progressback) {
234239
var result = defer();
235240

@@ -268,7 +273,6 @@ function qFactory(nextTick, exceptionHandler) {
268273
return result.promise;
269274
},
270275
always: function(callback) {
271-
272276
function makePromise(value, resolved) {
273277
var result = defer();
274278
if (resolved) {
@@ -278,14 +282,14 @@ function qFactory(nextTick, exceptionHandler) {
278282
}
279283
return result.promise;
280284
}
281-
285+
282286
function handleCallback(value, isResolved) {
283-
var callbackOutput = null;
287+
var callbackOutput = null;
284288
try {
285289
callbackOutput = (callback ||defaultCallback)();
286290
} catch(e) {
287291
return makePromise(e, false);
288-
}
292+
}
289293
if (callbackOutput && callbackOutput.then) {
290294
return callbackOutput.then(function() {
291295
return makePromise(value, isResolved);
@@ -296,7 +300,7 @@ function qFactory(nextTick, exceptionHandler) {
296300
return makePromise(value, isResolved);
297301
}
298302
}
299-
303+
300304
return this.then(function(value) {
301305
return handleCallback(value, true);
302306
}, 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)