2
2
// callbacks, then executes them serially with respect to async. This is to
3
3
// avoid deep nesting of callbacks in tests.
4
4
//
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
+ //
5
8
// After last successful navigation, asyncTest is automatically resumed.
6
9
//
7
10
// Usage:
@@ -30,8 +33,9 @@ function navigate(frame) {
30
33
var target = item [ 0 ] , callback = item [ 1 ]
31
34
32
35
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 )
35
39
} )
36
40
37
41
if ( typeof target == "number" ) {
@@ -45,3 +49,18 @@ function navigate(frame) {
45
49
46
50
return api
47
51
}
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