Basic Angular fiddle
Skeleton fiddle for Angular.
HTML
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.js"></script>
<div ng-app="app">
<div ng-controller='MainCtrl'>
<h1>Chained i {{i}}</h1>
</div>
</div>
JavaScript
angular.module('app', [])
.controller('MainCtrl', ['$scope', '$q', function ($scope, $q) {
var promise = $q.when(1);
var result = promise.then(function (i) {
return i + 1;
})
.then(function (i) {
return i + 1;
})
.then(function (i) {
return i + 1;
});
result.then(function (i) {
console.log("Value of i:" + i);
$scope.i = i;
});
var errorPromise = $q.reject("error");
var resultError = errorPromise.then(function (data) {
return "success";
}, function (e) {
return e; // If uncommented the next chained promise will go to success.
//return $q.reject(e);
})
resultError.then(function (data) {
console.log("In success with data:" + data);
}, function (e) {
console.log("In error with error:" + e);
});
}]);