Deferred.js
6.42 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
dojo.provide("dojo.tests._base.Deferred");
var delay = function(ms){
var d = new dojo.Deferred();
ms = ms || 20;
if(this.setTimeout){
setTimeout(function(){
d.progress(0.5);
},ms/2);
setTimeout(function(){
d.resolve();
},ms);
}else{
d.progress(0.5);
d.resolve();
}
return d.promise;
};
doh.register("tests._base.Deferred",
[
function callback(t){
var nd = new dojo.Deferred();
var cnt = 0;
nd.addCallback(function(res){
doh.debug("debug from dojo.Deferred callback");
return res;
});
nd.addCallback(function(res){
// t.debug("val:", res);
cnt+=res;
return cnt;
});
nd.callback(5);
// t.debug("cnt:", cnt);
t.assertEqual(cnt, 5);
},
function callback_extra_args(t){
var nd = new dojo.Deferred();
var cnt = 0;
nd.addCallback(dojo.global, function(base, res){ cnt+=base; cnt+=res; return cnt; }, 30);
nd.callback(5);
t.assertEqual(cnt, 35);
},
function errback(t){
var nd = new dojo.Deferred();
var cnt = 0;
nd.addErrback(function(val){
return ++cnt;
});
nd.errback();
t.assertEqual(cnt, 1);
},
function callbackTwice(t){
var nd = new dojo.Deferred();
var cnt = 0;
nd.addCallback(function(res){
return ++cnt;
});
nd.callback();
t.assertEqual(cnt, 1);
var thrown = false;
try{
nd.callback();
}catch(e){
thrown = true;
}
t.assertTrue(thrown);
},
function addBoth(t){
var nd = new dojo.Deferred();
var cnt = 0;
nd.addBoth(function(res){
return ++cnt;
});
nd.callback();
t.assertEqual(cnt, 1);
// nd.callback();
// t.debug(cnt);
// t.assertEqual(cnt, 1);
},
function callbackNested(t){
var nd = new dojo.Deferred();
var nestedReturn = "yellow";
nd.addCallback(function(res){
nd.addCallback(function(res2){
nestedReturn = res2;
});
return "blue";
});
nd.callback("red");
t.assertEqual("blue", nestedReturn);
},
function simpleThen(t){
var td = new doh.Deferred();
delay().then(function(){
td.callback(true);
});
return td;
},
function thenChaining(t){
var td = new doh.Deferred();
var p = delay();
var p2 = p.then(function(){
return 1;
});
p3 = p2.then(function(){
return 2;
});
p3.then(function(){
p2.then(function(v){
t.assertEqual(v, 1);
p3.then(function(v){
t.assertEqual(v, 2);
td.callback(true);
});
});
});
return td;
},
function simpleWhen(t){
var td = new doh.Deferred();
dojo.when(delay(), function(){
td.callback(true);
});
return td;
},
function progress(t){
if(dojo.isBrowser){
var td = new doh.Deferred();
var percentDone;
dojo.when(delay(), function(){
t.is(percentDone, 0.5);
td.callback(true);
},function(){},
function(completed){
percentDone = completed;
});
return td;
}
return null;
},
function cancelThenDerivative(t){
var def = new dojo.Deferred();
var def2 = def.then();
try{
def2.cancel();
t.t(true); // Didn't throw an error
}catch(e){
t.t(false);
}
},
function cancelPromiseValue(t){
var cancelledDef;
var def = new dojo.Deferred(function(_def){ cancelledDef = _def; });
def.promise.cancel();
t.is(def, cancelledDef);
},
function errorResult(t){
var def = new dojo.Deferred();
var result = new Error("rejected");
def.reject(result);
t.is(def.fired, 1);
t.is(def.results[1], result);
},
function globalLeak(t){
var def = new dojo.Deferred();
def.then(function(){ return def; });
def.resolve(true);
t.is(dojo.global.results, undefined, "results is leaking into global");
t.is(dojo.global.fired, undefined, "fired is leaking into global");
},
function backAndForthProcess(t){
var def = new dojo.Deferred();
var retval = "fail";
def.addErrback(function(){
return "ignore error and throw this good string";
}).addCallback(function(){
throw new Error("error1");
}).addErrback(function(){
return "ignore second error and make it good again";
}).addCallback(function(){
retval = "succeed";
});
def.errback("");
t.assertEqual("succeed", retval);
},
function backAndForthProcessThen(t){
var def = new dojo.Deferred;
var retval = "fail";
def.then(null, function(){
return "ignore error and throw this good string";
}).then(function(){
throw "error1";
}).then(null, function(){
return "ignore second error and make it good again";
}).then(function(){
retval = "succeed";
});
def.reject("");
t.assertEqual("succeed", retval);
},
function returnErrorObject(t){
var def = new dojo.Deferred();
var retval = "fail";
def.addCallback(function(){
return new Error("returning an error should work same as throwing");
}).addErrback(function(){
retval = "succeed";
});
def.callback();
t.assertEqual("succeed", retval);
},
function returnErrorObjectThen(t){
var def = new dojo.Deferred();
var retval = "fail";
def.then(function(){
return new Error("returning an error should NOT work same as throwing");
}).then(function(){
retval = "succeed";
});
def.resolve();
t.assertEqual("succeed", retval);
},
function errbackWithPromise(t){
var def = new dojo.Deferred();
var retval;
def.addCallbacks(function(){}, function(err){
return err;
});
def.promise.then(
function(){ retval = "fail"; },
function(){ retval = "succeed"; });
def.errback(new Error);
t.assertEqual("succeed", retval);
},
function testDojoPromiseProgressBasic(t){
var a = new dojo.Deferred();
var b = new dojo.Deferred();
var called = false;
a.then(function(){
b.then(function(){
if (!called){
console.log("Boo. ProgressBasic not called");
}
}, function(){
console.log("Unexpected");
}, function(){
called = true;
console.log("Yay. ProgressBasic called");
});
});
a.resolve();
b.progress();
b.resolve();
t.t(called);
},
function testDojoPromiseProgressChain(t){
var a = new dojo.Deferred();
var b = new dojo.Deferred();
var called = false;
a.then(function(){
return b;
}).then(function(){
if (!called){
console.log("Boo. ProgressChain not called");
}
}, function(){
console.log("Unexpected");
}, function(){
called = true;
console.log("Yay. ProgressChain called");
});
a.resolve();
b.progress();
b.resolve();
t.t(called);
}
]
);