-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdebug.js
221 lines (197 loc) · 4.53 KB
/
debug.js
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
// @ts-check
// debug 2.3.3.3.1
const adapter = require('./test/adapter')
// @ts-ignore
global.adapter = adapter
const thenables = require('./test/aplus-tests/helpers/thenables')
var resolved = adapter.resolved
var deferred = adapter.deferred
var dummy = { dummy: 'dummy' } // we fulfill or reject with this when we don't intend to test against it
var sentinel = { sentinel: 'sentinel' } // a sentinel fulfillment value to test for with strict equality
var other = { other: 'other' } // a value we don't want to be strict equal to
// unwrap check if onFullfilled called
const testFullfillTwice = () => {
adapter
.resolved(
thenables.fulfilled['a thenable that tries to fulfill twice'](
thenables.fulfilled['an asynchronously-fulfilled custom thenable'](1),
),
)
.then(v => {
console.log('final result', v)
})
.catch(e => {
console.log('error', e)
})
}
// already-fulfilled promise
const testAlreadyFullfilled = () => {
adapter
.resolved(
thenables.fulfilled['an asynchronously-fulfilled custom thenable'](
thenables.fulfilled['an already-fulfilled promise'](1),
),
)
.then(v => {
console.log('final result', v)
})
.catch(e => {
console.log('error', e)
})
const p100 = adapter.resolved(100)
setTimeout(() => {
p100.then(v => {
console.log('p100', v)
})
console.log('after p100')
}, 100)
}
const testUnwrapThrows = () => {
adapter
.resolved(
thenables.fulfilled['a thenable that fulfills but then throws'](
thenables.fulfilled['an asynchronously-fulfilled custom thenable'](1),
),
)
.then(v => {
console.log('final result', v)
})
.catch(e => {
console.log('error', e)
})
}
const testResolveNestedThenablesThatFullfillsTwice = () => {
adapter
.resolved(
thenables.fulfilled['a thenable that tries to fulfill twice'](
// thenables.fulfilled['an already-fulfilled promise'](1),
1,
),
)
.then(v => {
console.log('final result', v)
})
.catch(e => {
console.log('error', e)
})
}
const testRejectSelf = () => {
const p1 = adapter.rejected(1).catch(err => {
return p1
// return adapter.rejected(2)
// throw adapter.rejected(2)
// return adapter.resolved(10)
})
p1.then(v => {
console.log('val', v)
}).catch(err => {
console.log('err', err)
})
}
const testOnFulfilledThrow = () => {
adapter
.resolved()
.then(() => {
throw null
})
.catch(e => {
console.log(e)
})
}
const testRetrivingThen = () => {
const thenable = {
then: () => {
throw new Error('not this')
},
}
const func = () => {}
func.then = thenable.then
const bo = true
// @ts-ignore
bo.then = thenable.then
adapter
.resolved()
.then(() => {
// return thenable
// return func
return bo
})
.then(v => {
console.log(v)
})
.catch(e => {
console.log('err', e)
})
}
const testReject = () => {
const deferred = adapter.deferred()
// not ok
// deferred.resolve(deferred.promise)
// ok
deferred.reject(deferred.promise)
deferred.promise
.then(v => {
console.log(v)
})
.catch(e => {
console.log('err', e)
})
}
const testThenableReject = () => {
const thenable = {
then: (cb, onRejected) => {
// cb(1)
throw new Error('not this')
// onRejected(2)
// onRejected(adapter.resolved(2))
},
}
adapter
.resolved(1)
.then(v => {
return thenable
})
.then(v => {
console.log('> result:', v)
})
.catch(e => {
console.log('> error:', e)
})
}
// calling `resolvePromise` with an asynchronously-fulfilled promise, then calling " +"`rejectPromise`, both synchronously
const testResolveThenReject = () => {
function xFactory() {
var d = adapter.deferred()
setTimeout(function() {
d.resolve({ s: 1 })
}, 50)
return {
then: function(resolvePromise, rejectPromise) {
resolvePromise(d.promise)
rejectPromise(other)
},
}
}
adapter
.resolved({ x: 1 })
// .rejected()
.then(() => {
return xFactory()
})
.then(v => {
console.log('> result:', v)
})
.catch(e => {
console.log('> error:', e)
})
}
// testFullfillTwice()
// testAlreadyFullfilled()
// testUnwrapThrows()
// testResolveNestedThenablesThatFullfillsTwice()
// testRejectSelf()
// testOnFulfilledThrow()
// testRetrivingThen()
// testReject()
// testThenableReject()
testResolveThenReject()