Created
May 14, 2014 13:03
-
-
Save progmonster/fd45331c15a6fa4b2c01 to your computer and use it in GitHub Desktop.
Example of AngularJS promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html ng-app="app"> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> | |
<script src="js/index.js"></script> | |
</head> | |
<body> | |
<div ng-controller="TestController"> | |
<input type="button" value="click" ng-click="test()"> | |
<p>task 1 {{task1Status}}</p> | |
<p>task 2 {{task2Status}}</p> | |
<p>task 3 {{task3Status}}</p> | |
<p>all task {{allTaskStatus}}</p> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('app', []); | |
app.controller('TestController', function($scope, $q) { | |
$scope.test = function() { | |
var deferred1 = $q.defer(); | |
var deferred2 = $q.defer(); | |
var deferred3 = $q.defer(); | |
setTimeout(function() { | |
$scope.task1Status = "Done"; | |
deferred1.resolve(); | |
}, 5000); | |
setTimeout(function() { | |
$scope.task2Status = "Done"; | |
deferred2.resolve(); | |
}, 10000); | |
setTimeout(function() { | |
$scope.task3Status = "Done"; | |
deferred3.resolve(); | |
}, 15000); | |
$q.all([deferred1.promise, deferred2.promise, deferred3.promise]) | |
.then(function(res) { | |
$scope.allTaskStatus = "Done"; | |
}, function(err) { | |
}); | |
}; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment