mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-28 12:55:48 +08:00
fix($q): make $q.reject support finally and catch
Add support for the functions `finally` and `catch` to the promise returned by `$q.reject` Closes #6048 Closes #6076
This commit is contained in:
committed by
Caitlin Potter
parent
5ed721b9b5
commit
074b0675a1
@@ -223,7 +223,7 @@ function qFactory(nextTick, exceptionHandler) {
|
||||
|
||||
|
||||
reject: function(reason) {
|
||||
deferred.resolve(reject(reason));
|
||||
deferred.resolve(createInternalRejectedPromise(reason));
|
||||
},
|
||||
|
||||
|
||||
@@ -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 createInternalRejectedPromise = function(reason) {
|
||||
return {
|
||||
then: function(callback, errback) {
|
||||
var result = defer();
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user