Skip to content

Commit

Permalink
Add promise support (close #329, close #1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
travisjeffery committed Mar 13, 2014
1 parent a166284 commit a2968bf
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ Runnable.prototype.run = function(fn){

// timeout
if (this.async) {
if (ms) {
this.timer = setTimeout(function(){
this.timer = setTimeout(function(){
if (!finished) {
done(new Error('timeout of ' + ms + 'ms exceeded'));
self.timedOut = true;
}, ms);
}
}
}, ms);
}

// called multiple times
Expand Down Expand Up @@ -218,10 +218,22 @@ Runnable.prototype.run = function(fn){

// sync
try {
if (!this.pending) this.fn.call(ctx);
this.duration = new Date - start;
fn();
if (!this.pending) {
var result = this.fn.call(ctx);
if (result && typeof result.then === 'function') {
result.then(
function(){
done();
},
done
);
} else {
done();
}
} else {
done();
}
} catch (err) {
fn(err);
done(err);
}
};

0 comments on commit a2968bf

Please sign in to comment.