This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.html
116 lines (102 loc) · 3.7 KB
/
promise.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
<!doctype html>
<html>
<head>
<title>Promise with squishy-pants.js</title>
<meta charset="utf-8" />
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="../squishy-pants.js"></script>
<script>
function event(element, name, callback) {
if(element.addEventListener) {
element.addEventListener(name, callback, false);
} else {
element.attachEvent('on' + name, callback);
}
}
event(window, 'load', function() {
var _ = squishy,
log = _.IO(_.constant(function(x) {
document.getElementById('output').innerHTML += x + '<br />';
})),
logger = _.curry(function(prefix, value) {
log.ap(_.IO.of(prefix + ' > ' + value)).unsafePerform();
}),
// Normal promise
promise = _.Promise(function(resolve, reject) {
setTimeout(function() {
resolve(_.arb(String, 20));
}, 100);
}),
// Once promise with stateful deferred
once = _.once(
function() {
return _.arb(String, 20);
}
),
oncePromise = _.Promise(function(resolve, reject) {
resolve(once());
});
// Once async promise with stateful deferred
asyncOnce = _.asyncOnce(
function(resolve) {
setTimeout(function() {
resolve(_.arb(String, 20));
}, 100);
}
),
asyncOncePromise = _.Promise(function(resolve, reject) {
asyncOnce(resolve);
});
// Normal promise listener
promise.fork(
logger('A - Normal Immediate success'),
logger('A - Normal Immediate failure')
);
// Once promise listener
oncePromise.fork(
logger('B - Once Immediate success'),
logger('B - Once Immediate failure')
);
// Once async promise listener
asyncOncePromise.fork(
logger('C - Once Async Immediate success'),
logger('C - Once Async Immediate failure')
);
// Wait to call again
setTimeout(function() {
promise.fork(
logger('A - Normal Asynchronous success'),
logger('A - Normal Asynchronous failure')
);
oncePromise.fork(
logger('B - Once Asynchronous success'),
logger('B - Once Asynchronous failure')
);
asyncOncePromise.fork(
logger('C - Once Async Asynchronous success'),
logger('C - Once Async Asynchronous failure')
);
}, 200);
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="span8 offset2">
<h1>Promise using <a href="https://github.com/SimonRichardson/squishy-pants">squishy-pants.js</a></h1>
<hr />
<div class="alert">
<p>
Note: The once & asyncOnce deferred is only called once, where as the normal
deferred is called each time the fork is called. With this in mind each resulting
string for normal deferred is then different.
</p>
</div>
<hr />
<div class="alert" id="output"></div>
</div>
</div>
</div>
</body>
</html>