defer.js
2.61 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
var Q = require('../kew')
// create a deferred which returns a promise
exports.testDeferredResolve = function (test) {
var val = "ok"
var defer = Q.defer()
defer.promise
.then(function (data) {
test.equal(data, val, "Promise successfully returned")
test.done()
})
setTimeout(function () {
defer.resolve(val)
}, 50)
}
// make sure a deferred can only resolve once
exports.testDeferredResolveOnce = function (test) {
var defer = Q.defer()
try {
defer.resolve(true)
defer.resolve(true)
test.fail("Unable to resolve the same deferred twice")
} catch (e) {
}
test.done()
}
// create a deferred which returns a failed promise
exports.testDeferredReject = function (test) {
var err = new Error("hello")
var defer = Q.defer()
defer.promise
.fail(function (e) {
test.equal(e, err, "Promise successfully failed")
test.done()
})
setTimeout(function () {
defer.reject(err)
}, 50)
}
// make sure a deferred can only reject once
exports.testDeferredRejectOnce = function (test) {
var defer = Q.defer()
try {
defer.reject(new Error("nope 1"))
defer.reject(new Error("nope 2"))
test.fail("Unable to reject the same deferred twice")
} catch (e) {
}
test.done()
}
// make sure a deferred can only reject once
exports.testDeferAndRejectFail = function (test) {
var defer
try {
defer = Q.defer()
defer.reject(new Error("nope 1"))
defer.resolve(true)
test.fail("Unable to reject and resolve the same deferred")
} catch (e) {
test.ok(true, "Unable to reject and resolve same deferred")
}
try {
defer = Q.defer()
defer.resolve(true)
defer.reject(new Error("nope 1"))
test.fail("Unable to reject and resolve the same deferred")
} catch (e) {
test.ok(true, "Unable to reject and resolve same deferred")
}
test.done()
}
// create a deferred which resolves with a node-standard callback
exports.testDeferredResolverSuccess = function (test) {
var val = "ok"
var defer = Q.defer()
var callback = defer.makeNodeResolver()
defer.promise
.then(function (data) {
test.equal(data, val, "Promise successfully returned")
test.done()
})
setTimeout(function () {
callback(null, val)
}, 50)
}
// create a deferred which rejects with a node-standard callback
exports.testDeferredResolverSuccess = function (test) {
var err = new Error("hello")
var defer = Q.defer()
var callback = defer.makeNodeResolver()
defer.promise
.fail(function (e) {
test.equal(e, err, "Promise successfully failed")
test.done()
})
setTimeout(function () {
callback(err)
}, 50)
}