Skip to content

Commit e765fd8

Browse files
committed
Add rudimentary Promise support to navigate helper
1 parent 4478fb8 commit e765fd8

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

test/unit/helpers.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// callbacks, then executes them serially with respect to async. This is to
33
// avoid deep nesting of callbacks in tests.
44
//
5+
// If a `then`able object is returned from a callback, then the navigation will
6+
// resume only after the promise has been resolved.
7+
//
58
// After last successful navigation, asyncTest is automatically resumed.
69
//
710
// Usage:
@@ -30,8 +33,9 @@ function navigate(frame) {
3033
var target = item[0], callback = item[1]
3134

3235
frame.$(frame.document).one("pjax:end", function() {
33-
if (callback) callback(frame)
34-
setTimeout(workOff, 0)
36+
var promise = callback && callback(frame)
37+
if (promise && typeof promise.then == "function") promise.then(workOff)
38+
else setTimeout(workOff, 0)
3539
})
3640

3741
if (typeof target == "number") {
@@ -45,3 +49,18 @@ function navigate(frame) {
4549

4650
return api
4751
}
52+
53+
// A poor man's Promise implementation. Only resolvable promises with no
54+
// reject/catch support.
55+
function PoorMansPromise(setup) {
56+
var result, callback, i = 0, callbacks = []
57+
setup(function(_result) {
58+
result = _result
59+
while (callback = callbacks[i++]) callback(result)
60+
})
61+
this.then = function(done) {
62+
if (i == 0) callbacks.push(done)
63+
else setTimeout(function(){ done(result) }, 0)
64+
return this
65+
}
66+
}

0 commit comments

Comments
 (0)