-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
245 lines (228 loc) · 8.37 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<html ng-app="app">
<head>
<link rel="stylesheet" href="lib/reveal.js/css/reveal.css">
<link rel="stylesheet" href="lib/reveal.js/css/theme/white.css">
<link href="/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<link href="/lib/bootstrap/dist/css/bootstrap-theme.css" rel="stylesheet" />
<title> Dwayne Page </title>
</head>
<body ng-controller="myController">
<div class="reveal">
<div class="slides">
<section>
<h3>
Promises <br />
</h3>
</section>
<section>
Promise - proposed 1976 by Daniel P. Friedman. The value
is unknown because the computation is not complete.
</section>
<section>
A javascript object that will, eventually, return a value. Your code does not wait.
</section>
<section>
<section>Different standards and Implementations. </section>
<section> Promises/A <br />
http://wiki.commonjs.org/wiki/Promises/A
</section>
<section>
Promises/A+ <br />
https://promisesaplus.com/
</section>
<section>
bluebird <br />
http://bluebirdjs.com/docs/getting-started.html
</section>
<section>
q, the one used by angular <br />
https://github.com/kriskowal/q
</section>
<section>
ES6 (ECMAScript, ie javascript) <br />
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise <br />
This will likely be the standard in the future, part of core javascript.
</section>
</section>
<section>
<h3>States and fates</h3>
<ol>
<li> fulfilled</li>
<li> rejected </li>
<li> pending </li>
</ol>
<h4> Fates </h4>
<ol>
<li>
resolved
</li>
<li> unresolved</li>
</ol>
</section>
<section>
Resolved promises can be
<div>
fulfilled, rejected, or pending.
</div>
<p>
Fulfilled if it is finished, rejected if it has been rejected or is currently rejecting.
Pending if it has been resolved to another promise.
</p>
</section>
<section>
Unresolved promises are pending.
</section>
<section>
<h3> Commonly found misused idiom </h3>
$http.get returns a promise already, there is <i>almost always</i> no need to wrap in $q.defer(). This is the most
commonly found misuse. Not really bad, just superflous.
</section>
<section>
This is called the 'deferred antipattern' or 'Promise constructor antipattern'
</section>
<section>
<div class="row">
<div class="col-lg-6">
<h3>A simple promise</h3>
<pre>
$scope.getData = function (url) {
return $http.get(url).then(
function success(response) {
$scope.worked = true
},
function failure(reason) {
$scope.worked = false
}
)
}
</pre>
</div>
<div class="col-lg-6">
<button class="btn btn-default" ng-click="getData('/common/SampleData.json')">Works</button>
<button class="btn btn-default" ng-click="getData('/nowork')">Fail, wrong URL</button>
<div> <h3>{{ worked }}</h3> </div>
</div>
</div> <!-- End Row -->
</section>
<section>
<div class="row">
<div class="col-lg-6">
<h3> Promise Anti Pattern </h3>
<pre>
// In the Controller
$scope.getFromFactory = function (url) {
myFactory.getData(url).then(
function success() {
$scope.worked = "Working"
}, function failure() {
$scope.worked = "Not working"
})
}
// In the factory
getData: function (url) {
return $http.get(url).then(
function success(response) {
return response.data
},
function failure(reason) {
return reason // Does not return promise
}
)
}
</pre>
</div>
<div class="col-lg-6">
<h3></h3>
<button class="btn btn-default" ng-click="getFromFactory('/common/SampleData.json')">Works</button>
<button class="btn btn-default" ng-click="getFromFactory('/nowork')">Fail, wrong URL</button>
<div> <h3>{{ worked }}</h3> </div>
</div>
</div>
</section>
<section>
<div class="row">
<div class="col-lg-6">
<h3> Better </h3>
<pre>
// In the Controller
$scope.getFromFactoryGOOD = function (url) {
myFactory.getDataGOOD(url).then(
function success() {
$scope.worked = true
}, function failure() {
$scope.worked = false
})
}
// In the factory
getDataGOOD: function (url) {
return $http.get(url).then(
function success(response) {
console.log(response.data)
return response.data
}
)
}
</pre>
</div>
<div class="col-md-6">
<h3></h3>
<button class="btn btn-default" ng-click="getFromFactoryGOOD('/common/SampleData.json')">Works</button>
<button class="btn btn-default" ng-click="getFromFactoryGOOD('/nowork')">Fail, wrong URL</button>
<div> <h3>{{ worked }}</h3> </div>
</div>
</div> <!-- End Row -->
</section>
<section>
<div class="row">
<div class="col-lg-6">
Best!!
<pre>
function initialize() {
var _url = "/common/timeZoneData.json";
return $http({
method: 'GET',
url: _url
}).then(function successCallback(response) {
for (var i = 0; i < response.data.zones.length; i++) {
moment.tz.add(response.data.zones[i]);
}
}).then(function () {
return getTasks()
}).then(function () {
return addLate()
}).catch(function () {
sweetAlertFactory.note("Error getting time zone information!");
})
}
</pre>
</div>
<div class="col-md-6">
<h3>Full circle back to synchronous code? Not quite, no waiting</h3>
</div>
</div> <!-- End Row -->
</section>
<section>
<p>
Every .then returns a function. This maintains the promise so it is not resolved prematurely.
</p>
</section>
<section>
Catch Method is used. Any failed promise in the chain will break the chain and immediately proceed to the catch method.
</section>
<section>
Avoids .success and .failure which are convenience methods for $http
</section>
</div>
</div>
<script src="lib/reveal.js/js/reveal.js"></script>
<script>
Reveal.initialize();
</script>
<script type="text/javascript" src="lib/reveal.js/js/reveal.js"></script>
<script type="text/javascript" src="lib/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="lib/angular/angular.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="myController.js"></script>
<script type="text/javascript" src="myFactory.js"></script>
</body>
</html>