promise example
this example shows how to notify any message though defer object until any resolve or reject message executes.
by Samar Pattanayak
HTML
<div ng-app="myApp">
<div ng-controller="myController">
{{age}}
</div>
</div>
JavaScript
var ang = angular.module("myApp", []);
ang.controller("myController", function($scope, myFactory) {
$scope.age = 23;
$scope.checkQ = function() {
myFactory.method1(4000).then(function(res, d) {
console.log(res);
}, function(resErr, d) {
console.log(resErr);
}, function(not) {
console.log(not)
})
};
$scope.checkQ();
myFactory.method2();
})
ang.service("myFactory", function($timeout, $q, $interval) {
var first = 91;
var defer = $q.defer();
this.method1 = function(time) {
$timeout(function() {
if (first == 90) {
defer.resolve("Success");
} else {
defer.reject("Rejected");
}
}, time)
var value = 1;
$timeout(function() {
defer.notify("Started Now");
}, 0);
for (var i = 0; i < time / 1000; i++) {
$timeout(function() {
defer.notify("hi" + value++);
}, i * 1000);
}
return defer.promise;
},
this.method2 = function() {
console.log("hello");
}
})