Promise.js
1.69 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
define([
"doh/main",
"dojo/Deferred"
], function(doh, Deferred){
var tests = {
"always() will be invoked for resolution and rejection": function(t){
var obj = {};
var deferred1 = new Deferred();
var thenResult, alwaysResult;
deferred1.promise.then(function(result){ thenResult = result; });
deferred1.promise.always(function(result){ alwaysResult = result; });
deferred1.resolve(obj);
t.t(alwaysResult === obj);
t.t(alwaysResult === thenResult);
var deferred2 = new Deferred();
var thenResult2, alwaysResult2;
deferred2.promise.then(null, function(result){ thenResult2 = result; });
deferred2.promise.always(function(result){ alwaysResult2 = result; });
deferred2.reject(obj);
t.t(alwaysResult2 === obj);
t.t(alwaysResult2 === thenResult2);
},
"otherwise(…) is equivalent to then(null, …)": function(t){
var obj = {};
var thenResult, otherwiseResult;
this.deferred.then(null, function(result){ thenResult = result; });
this.deferred.promise.otherwise(function(result){ otherwiseResult = result; });
this.deferred.reject(obj);
t.t(otherwiseResult === obj);
t.t(otherwiseResult === thenResult);
},
"trace() returns the same promise": function(t){
var promise = this.deferred.promise.trace();
t.t(promise === this.deferred.promise);
},
"traceRejected() returns the same promise": function(t){
var promise = this.deferred.promise.traceRejected();
t.t(promise === this.deferred.promise);
}
};
var wrapped = [];
for(var name in tests){
wrapped.push({
name: name,
setUp: setUp,
runTest: tests[name]
});
}
function setUp(){
this.deferred = new Deferred;
}
doh.register("tests.promise.Promise", wrapped);
});