Skip to content

Commit

Permalink
Merge pull request #18 from rhagigi/synchronousResolve
Browse files Browse the repository at this point in the history
Synchronously call callbacks added to promise before resolves/rejects
  • Loading branch information
amay authored Jul 17, 2016
2 parents 7f6c21a + 50ac3b5 commit fb16495
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function buildThenable() {
return {
onFulfilled: [],
onRejected: [],
onFinally: [],
then: function(onFulfill, onReject) {
try {
if (this.resolved && !this.rejected) {
Expand Down Expand Up @@ -28,6 +31,12 @@ function buildThenable() {
if (this.rejected && onReject) {
return this.catch(onReject);
}
if (!this.rejected && onFulfill) {
this.onFulfilled.push(onFulfill);
}
if (!this.resolved && onReject) {
this.onRejected.push(onReject);
}
return this;
},

Expand All @@ -44,13 +53,18 @@ function buildThenable() {
}
return this;
}
if (!this.resolved) {
this.onRejected.push(onReject);
}
return this;
},

finally: function(callback) {
if (this.resolved || this.rejected) {
callback();
return;
}
this.onFinally.push(callback);
}
};
}
Expand All @@ -60,13 +74,23 @@ function setup(sinon) {
this.thenable.resolved = true;
this.thenable.rejected = false;
this.thenable.resolveValue = value;
this.thenable.onFulfilled
.concat(this.thenable.onFinally)
.forEach(function(callback) {
callback(value);
});
return this;
}

function rejects(value) {
this.thenable.rejected = true;
this.thenable.resolved = false;
this.thenable.rejectValue = value;
this.thenable.onRejected
.concat(this.thenable.onFinally)
.forEach(function(callback) {
callback(value);
});
return this;
}

Expand Down
55 changes: 55 additions & 0 deletions spec/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ describe('stubPromise', function() {

expect(resolved).to.be.false;
});
it('does resolve synchronously when explicitly resolved AFTER a then is added', function() {
var resolved = false;

promise().then(function() {
resolved = true;
});

expect(resolved).to.be.false;
promise.resolves();
expect(resolved).to.be.true;
});

it('returns stub from resolves call', function(done) {
var stub = promise.resolves();
Expand Down Expand Up @@ -120,6 +131,28 @@ describe('stubPromise', function() {

expect(rejected).to.be.false;
});
it('can reject via catch synchronously when explicitly rejected AFTER a catch is added', function() {
var rejected = false;

promise().catch(function() {
rejected = true;
});

expect(rejected).to.be.false;
promise.rejects();
expect(rejected).to.be.true;
});
it('can reject via a then reject function synchronously when explicitly rejected AFTER a then reject function is added', function() {
var rejected = false;

promise().then(f, function() {
rejected = true;
});

expect(rejected).to.be.false;
promise.rejects();
expect(rejected).to.be.true;
});

it('returns stub from rejects call', function(done) {
var stub = promise.rejects();
Expand Down Expand Up @@ -177,6 +210,17 @@ describe('stubPromise', function() {
expect(finallyCalled).to.be.true;
});

it('invokes finally when promise is resolved after the finally is added', function() {
var finallyCalled = false;
promise().finally(function() {
finallyCalled = true;
});

expect(finallyCalled).to.be.false;
promise.resolves();
expect(finallyCalled).to.be.true;
});

it('invokes finally when promise is rejected', function() {
promise.rejects();

Expand All @@ -188,6 +232,17 @@ describe('stubPromise', function() {
expect(finallyCalled).to.be.true;
});

it('invokes finally when promise is rejected after the finally is added', function() {
var finallyCalled = false;
promise().finally(function() {
finallyCalled = true;
});

expect(finallyCalled).to.be.false;
promise.rejects();
expect(finallyCalled).to.be.true;
});

it('handles catches that succeed', function(done) {
promise.rejects('an error');

Expand Down

0 comments on commit fb16495

Please sign in to comment.