Skip to content

Commit

Permalink
Fixed thenable assimilation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Oct 11, 2011
1 parent b4552b2 commit 5adc525
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Future Backward Incompatible
This is more consistent with NodeJS and (subjectively)
more explicit and intuitive.

Next

- Fixed thenable promise assimilation.

0.7.1

- Stopped shimming ``Array.prototype.reduce``. The
Expand Down
14 changes: 3 additions & 11 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,9 @@ function ref(object) {
return object;
// assimilate thenables, CommonJS/Promises/A
if (object && typeof object.then === "function") {
return Promise({}, function fallback(op, rejected) {
if (op !== "when") {
return when(object, function (value) {
return ref(value).promiseSend.apply(undefined, arguments);
});
} else {
var result = defer();
object.then(result.resolve, result.reject);
return result.promise;
}
});
var result = defer();
object.then(result.resolve, result.reject);
return result.promise;
}
return Promise({
"when": function (rejected) {
Expand Down
54 changes: 48 additions & 6 deletions test/thenable.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@

var Q = require('../q');

exports['test assimilation'] = function (ASSERT, done) {
exports['test ref assimilation'] = function (ASSERT, done) {
Q.ref({
"then": function (resolved, rejected) {
resolved(10);
}
})
.then(function (value) {
ASSERT.equal(value, 10, 'thenable resolved');
return value + 10;
})
.then(function (value) {
ASSERT.equal(value, 20, 'thenable chained');
return value + 10;
})
.then(function (value) {
ASSERT.equal(value, 30, 'thenable chained again');
})
.fin(done);
}

exports['test when assimilation'] = function (ASSERT, done) {
Q.when({
"then": function (resolved, rejected) {
resolved(10);
}
}, function (value) {
ASSERT.ok(true, 'thenable resolved');
done();
}, function () {
ASSERT.ok(false, 'thenable rejected');
done();
ASSERT.equal(value, 10, 'thenable resolved');
return value + 10;
})
.then(function (value) {
ASSERT.equal(value, 20, 'thenable chained');
return value + 10;
})
.then(function (value) {
ASSERT.equal(value, 30, 'thenable chained again');
})
.fin(done);
}

exports['test ref assimilation and piplining'] = function (ASSERT, done) {
Q.ref({
"then": function (resolved, rejected) {
resolved([10]);
}
})
.get(0)
.then(function (value) {
ASSERT.equal(value, 10, 'thenable resolved');
return value + 10;
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
}

exports['test provision'] = function (ASSERT, done) {
Expand Down

0 comments on commit 5adc525

Please sign in to comment.