Skip to content

Commit

Permalink
Add rudimentary Promise support to navigate helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 2, 2015
1 parent 4478fb8 commit e765fd8
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions test/unit/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// callbacks, then executes them serially with respect to async. This is to
// avoid deep nesting of callbacks in tests.
//
// If a `then`able object is returned from a callback, then the navigation will
// resume only after the promise has been resolved.
//
// After last successful navigation, asyncTest is automatically resumed.
//
// Usage:
Expand Down Expand Up @@ -30,8 +33,9 @@ function navigate(frame) {
var target = item[0], callback = item[1]

frame.$(frame.document).one("pjax:end", function() {
if (callback) callback(frame)
setTimeout(workOff, 0)
var promise = callback && callback(frame)
if (promise && typeof promise.then == "function") promise.then(workOff)
else setTimeout(workOff, 0)
})

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

return api
}

// A poor man's Promise implementation. Only resolvable promises with no
// reject/catch support.
function PoorMansPromise(setup) {
var result, callback, i = 0, callbacks = []
setup(function(_result) {
result = _result
while (callback = callbacks[i++]) callback(result)
})
this.then = function(done) {
if (i == 0) callbacks.push(done)
else setTimeout(function(){ done(result) }, 0)
return this
}
}

0 comments on commit e765fd8

Please sign in to comment.