-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.html
142 lines (117 loc) · 2.96 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.1.css">
</head>
<body>
<h1>Tests</h1>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.0.1.js"></script>
<script>
function promise(func) {
var state = 'pending';
var promiseValue;
var startThen = null;
function resolve(value) {
try {
state = 'fulfilled';
promiseValue = value;
if (typeof(startThen) === 'function') {
startThen();
}
} catch (e) {
reject(e);
}
}
function reject(e) {
state = 'rejected';
promiseValue = e;
if (typeof(startThen) === 'function') {
startThen();
}
}
function runThen(onFulfilled, onRejected, thenResolve, thenReject) {
if (state === 'pending') {
startThen = function() {
runThen(onFulfilled, onRejected, thenResolve, thenReject);
};
return;
}
var thenFunc;
if (state === 'fulfilled') {
thenFunc = onFulfilled;
} else {
thenFunc = onRejected;
}
var value;
try {
value = thenFunc(promiseValue);
thenResolve(value);
} catch (e) {
// if (onRejected) {
// onRejected(e);
// }
thenReject(e);
}
}
this.then = function(onFulfilled, onRejected) {
return new promise(function (resolve, reject) {
runThen(onFulfilled, onRejected, resolve, reject);
});
}
func(resolve, reject);
}
QUnit.test('is function', function(assert) {
assert.equal(typeof(promise), 'function');
});
QUnit.test('promise returns resolve value', function(assert) {
function myPromise() {
return new promise(function(resolve, reject) {
resolve('test');
});
}
myPromise().then(function(value) {
assert.equal(value, 'test');
});
});
QUnit.test('promise returns reject value', function(assert) {
function myPromise() {
return new promise(function(resolve, reject) {
reject('test');
});
}
myPromise().then(null,function(value) {
assert.equal(value, 'test');
});
});
QUnit.test('then catching errors', function(assert) {
function myPromise() {
return new promise(function(resolve, reject) {
resolve('test');
});
}
myPromise().then(function(value) {
throw 'error';
}).then(null,function(error) {
assert.equal(error, 'error');
});
});
QUnit.test('promise works async', function(assert) {
var done = assert.async();
function myPromise() {
return new promise(function(resolve, reject) {
setTimeout(function() {
resolve('async');
}, 1000);
});
}
myPromise().then(function(value) {
assert.equal(value, 'async');
done();
});
});
</script>
</body>
</html>