forked from bennadel/JavaScript-Demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaked.htm
84 lines (58 loc) · 1.67 KB
/
naked.htm
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
<!doctype html>
<html ng-app="Demo">
<head>
<meta charset="utf-8" />
<title>
Handling Top-Level Errors In A Promise Workflow In AngularJS
</title>
</head>
<body ng-controller="AppController">
<h1>
Handling Top-Level Errors In A Promise Workflow In AngularJS
</h1>
<p>
The naked version.
</p>
<!-- Load scripts. -->
<script type="text/javascript" src="../../vendor/angularjs/angular-1.3.8.min.js"></script>
<script type="text/javascript">
// Create an application module for our demo.
var app = angular.module( "Demo", [] );
// -------------------------------------------------- //
// -------------------------------------------------- //
// I control the root of the application.
app.controller(
"AppController",
function( $q ) {
// In this version, our top-level request for the promise is naked and
// exposed. While it does return a promise in its happy-path, it raises
// an exception if invoked during some unexpected state.
loadSomething()
.then(
function handleResolve( value ) {
console.log( "Resolved!" );
console.log( value );
},
function handleReject( error ) {
console.log( "Rejected!" );
console.log( error );
}
)
;
// ---
// PRIVATE METHODS.
// ---
// I load some data and return a promise.
function loadSomething() {
// If the state of the service is such that now is not a good time to
// actually invoke the service, raises exception.
if ( this.someStateFlag !== 1 ) {
throw( new Error( "InvalidState" ) );
}
return( $q.when( "someValue" ) );
}
}
);
</script>
</body>
</html>