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>