91
91
* This method *returns a new promise* which is resolved or rejected via the return value of the
92
92
* `successCallback` or `errorCallback`.
93
93
*
94
+ * - `catch(errorCallback)` – shorthand for `promise.then(null, errorCallback)`
95
+ *
94
96
* - `always(callback)` – allows you to observe either the fulfillment or rejection of a promise,
95
97
* but to do so without modifying the final value. This is useful to release resources or do some
96
98
* clean-up that needs to be done whether the promise was rejected or resolved. See the [full
128
130
* you can treat promises attached to a scope as if they were the resulting values.
129
131
* - Q has many more features than $q, but that comes at a cost of bytes. $q is tiny, but contains
130
132
* all the important functionality needed for common async tasks.
131
- *
133
+ *
132
134
* # Testing
133
- *
135
+ *
134
136
* <pre>
135
137
* it('should simulate promise', inject(function($q, $rootScope) {
136
138
* var deferred = $q.defer();
137
139
* var promise = deferred.promise;
138
140
* var resolvedValue;
139
- *
141
+ *
140
142
* promise.then(function(value) { resolvedValue = value; });
141
143
* expect(resolvedValue).toBeUndefined();
142
- *
144
+ *
143
145
* // Simulate resolving of promise
144
146
* deferred.resolve(123);
145
147
* // Note that the 'then' function does not get called synchronously.
146
148
* // This is because we want the promise API to always be async, whether or not
147
149
* // it got called synchronously or asynchronously.
148
150
* expect(resolvedValue).toBeUndefined();
149
- *
151
+ *
150
152
* // Propagate promise resolution to 'then' functions using $apply().
151
153
* $rootScope.$apply();
152
154
* expect(resolvedValue).toEqual(123);
@@ -267,8 +269,13 @@ function qFactory(nextTick, exceptionHandler) {
267
269
268
270
return result . promise ;
269
271
} ,
272
+
273
+ "catch" : function ( callback ) {
274
+ return this . then ( null , callback ) ;
275
+ } ,
276
+
270
277
always : function ( callback ) {
271
-
278
+
272
279
function makePromise ( value , resolved ) {
273
280
var result = defer ( ) ;
274
281
if ( resolved ) {
@@ -278,14 +285,14 @@ function qFactory(nextTick, exceptionHandler) {
278
285
}
279
286
return result . promise ;
280
287
}
281
-
288
+
282
289
function handleCallback ( value , isResolved ) {
283
- var callbackOutput = null ;
290
+ var callbackOutput = null ;
284
291
try {
285
292
callbackOutput = ( callback || defaultCallback ) ( ) ;
286
293
} catch ( e ) {
287
294
return makePromise ( e , false ) ;
288
- }
295
+ }
289
296
if ( callbackOutput && callbackOutput . then ) {
290
297
return callbackOutput . then ( function ( ) {
291
298
return makePromise ( value , isResolved ) ;
@@ -296,7 +303,7 @@ function qFactory(nextTick, exceptionHandler) {
296
303
return makePromise ( value , isResolved ) ;
297
304
}
298
305
}
299
-
306
+
300
307
return this . then ( function ( value ) {
301
308
return handleCallback ( value , true ) ;
302
309
} , function ( error ) {
0 commit comments