Skip to content

Commit

Permalink
fix: catch promise inside of rejectUnlessResolvedWithin
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Dec 31, 2015
1 parent 8292020 commit ebb0704
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
39 changes: 20 additions & 19 deletions StateTransitionMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const BaseMethods = {
this.state = 'failed';
this._transitionPromise = undefined;
this._transition = undefined;

return Promise.reject(rejected);
},

Expand Down Expand Up @@ -164,16 +163,18 @@ function rejectUnlessResolvedWithin(promise, timeout) {
}, timeout);

return promise.then((fullfilled, rejected) => {
//console.log(`AA ${fullfilled} : ${rejected}`);
clearTimeout(th);
clearTimeout(th);

if (fullfilled) {
fullfill(fullfilled);
}
if (rejected) {
reject(rejected);
}
});
if (fullfilled) {
fullfill(fullfilled);
}
if (rejected) {
reject(rejected);
}
})
.catch(r => {
reject(r);
});
});
}

Expand Down Expand Up @@ -232,15 +233,15 @@ module.exports.defineActionMethods = function (object, actionsAndStates, enumera
this.state = this._transition.during;

this._transitionPromise = rejectUnlessResolvedWithin(this[privateActionName](), this._transition
.timeout)
.then(
resolved => {
this.state = this._transition.target;
this._transitionPromise = undefined;
this._transition = undefined;

return this;
}, rejected => this.stateTransitionRejection(rejected));
.timeout).then(
resolved => {

this.state = this._transition.target;
this._transitionPromise = undefined;
this._transition = undefined;

return this;
}, rejected => this.stateTransitionRejection(rejected));

return this._transitionPromise;
} else if (this._transition) {
Expand Down
18 changes: 16 additions & 2 deletions tests/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ class StatefullClass extends stm.StateTransitionMixin(BaseClass, actions, 'stopp
this.shouldReject = shouldReject;
}
_start() {
if (this.shouldReject) return Promise.reject(new Error("always reject"));

return new Promise((f, r) => {
setTimeout(() => {
if (this.shouldReject) {
r(new Error("always reject"));
r(Promise.reject(new Error("always reject")));
} else {
f(this);
}
Expand Down Expand Up @@ -135,7 +137,19 @@ describe('states', function () {
});
});

it('handle failure while starting', function (done) {
it('handle failure while starting without timeout guard', function (done) {
const o = new StatefullClass(0, true);

o.start().then((f, r) => {
console.log(`${f} ${r}`);
}).catch(e => {
//console.log(`catch: ${e}`);
assert.equal(o.state, 'failed');
done();
});
});

it('handle failure while starting with timeout guard', function (done) {
const o = new StatefullClass(10, true);

o.start().then((f, r) => {
Expand Down

0 comments on commit ebb0704

Please sign in to comment.