-
Notifications
You must be signed in to change notification settings - Fork 55
/
07-3-promises.html
54 lines (42 loc) · 1.1 KB
/
07-3-promises.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html>
<head>
<title>promises</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script>
<script>
var app = angular.module('app', []);
app.controller('BasicCtrl', function($scope, $q){
var defer = $q.defer();
//we create a defer object
//we promise a few functions using .then()
defer.promise
.then(function(){
console.log('hello');
})
.then(function(){
console.log('world');
});
//we then resolve the promises using .resolve()
defer.resolve();
var defer2 = $q.defer();
//promise a few functions
defer2.promise
.then(function(name){
//this function processed the name
return name.split('').reverse().join('');
//what is returned here will be passed to the next promise
})
.then(function(processedName){
//this function alerts the user
alert('Your name reversed is '+processedName);
});
//here we pass in the first parameter (i.e the persons name)
defer2.resolve('John Snow');
});
</script>
</head>
<body ng-app='app'>
<div ng-controller="BasicCtrl">
</div>
</body>
</html>