问题
I didn't understand the behavior of the return value of then
,
Here its documentation -
then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason.
So let's try it , according to the follow little code section -
var deferred = $q.defer();
deferred.reject();
deferred.promise.then(function () {
console.log("1st resolove");
},
function () {
console.log("1st reject");
}
).then(function () {
console.log("2nd resolve");
},
function () {
console.log("2nd reject");
}
);
1) Why does it log -
1st reject
2nd resolve
instead of -
1st reject
2nd reject
?
2) What do I have to change to make it log -
1st reject
2nd reject
?
var myAppModule = angular.module('myApp', []).
controller('myCtrl',function($scope,$q){
var deferred = $q.defer();
//deferred.resolve();
deferred.reject();
deferred.promise.then(function () {
console.log("1st resolove");
},
function () {
console.log("1st reject");
}
).then(function () {
console.log("2nd resolve");
},
function () {
console.log("2nd reject");
}
);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl"></div>
</div>
回答1:
You missed the next line of the docs:
This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback.
If you want the next chain to be the result of the same promise for whatever reason, return deferred.promise from inside your handler.
// This will always trigger the reject callback
deferred.promise.then(function () {
console.log("1st resolove");
return deferred.promise;
},
function () {
console.log("1st reject");
return deferred.promise;
}
)
If you want to return either the success/failed callback, depending on the result of the first, return $q.when()
inside your success and return $q.reject()
inside your failure:
deferred.promise.then(function () {
console.log("1st resolove");
return $q.when();
},
function () {
console.log("1st reject");
return $q.reject();
}
).then(function () {
console.log("2nd resolve");
},
function () {
console.log("2nd reject");
}
);
Example
来源:https://stackoverflow.com/questions/31020497/q-defer-then-of-the-then