Comparing version 0.2.1 to 0.2.2
42
kew.js
@@ -78,19 +78,23 @@ /** | ||
} | ||
return | ||
} | ||
} else if (data && isPromiseLike(data)) { | ||
data.then( | ||
function(data) { this.resolve(data) }.bind(this), | ||
function(err) { this.reject(err) }.bind(this) | ||
) | ||
} else { | ||
this._hasData = true | ||
this._data = data | ||
this._hasData = true | ||
this._data = data | ||
if (this._onComplete) { | ||
for (i = 0; i < this._onComplete.length; i++) { | ||
this._onComplete[i]() | ||
if (this._onComplete) { | ||
for (i = 0; i < this._onComplete.length; i++) { | ||
this._onComplete[i]() | ||
} | ||
} | ||
} | ||
if (this._promises) { | ||
for (i = 0; i < this._promises.length; i += 1) { | ||
this._promises[i]._withInput(data) | ||
if (this._promises) { | ||
for (i = 0; i < this._promises.length; i += 1) { | ||
this._promises[i]._withInput(data) | ||
} | ||
delete this._promises | ||
} | ||
delete this._promises | ||
} | ||
@@ -294,2 +298,13 @@ } | ||
/** | ||
* Return true iff the given object is a promise-like object, e.g. appears to | ||
* implement Promises/A+ specification | ||
* | ||
* @param {Object} obj The object to test | ||
* @return {boolean} Whether the object is a promise-like object | ||
*/ | ||
function isPromiseLike(obj) { | ||
return typeof obj === 'object' && typeof obj.then === 'function' | ||
} | ||
/** | ||
* Static function which creates and resolves a promise immediately | ||
@@ -442,4 +457,5 @@ * | ||
, isPromise: isPromise | ||
, isPromiseLike: isPromiseLike | ||
, resolve: resolve | ||
, reject: reject | ||
} |
{ | ||
"name": "kew" | ||
, "description": "a lightweight promise library for node" | ||
, "version": "0.2.1" | ||
, "version": "0.2.2" | ||
, "homepage": "https://github.com/Obvious/kew" | ||
@@ -6,0 +6,0 @@ , "authors": [ |
var Q = require('../kew') | ||
var originalQ = require('q') | ||
@@ -337,2 +338,26 @@ // test that fin() works with a synchronous resolve | ||
}) | ||
} | ||
} | ||
exports.testInteroperabilityWithOtherPromises = function(test) { | ||
var promise1 = Q.defer() | ||
promise1.then(function(value) { | ||
return originalQ(1 + value) | ||
}).then(function(result) { | ||
test.equal(result, 11) | ||
}) | ||
var promise2 = Q.defer(), | ||
errToThrow = new Error('error') | ||
promise2.then(function() { | ||
return originalQ.reject(errToThrow) | ||
}).fail(function(err) { | ||
test.equal(err, errToThrow) | ||
}) | ||
promise1.resolve(10) | ||
promise2.resolve() | ||
Q.all([promise1, promise2]).then(function() { | ||
test.done() | ||
}) | ||
} |
@@ -199,3 +199,3 @@ var Q = require('../kew') | ||
then: function (fn) { | ||
fn('still not a promise, brah') | ||
fn('like a promise, brah!') | ||
} | ||
@@ -208,1 +208,19 @@ } | ||
} | ||
// test checking whether something is a promise-like object | ||
exports.testIsPromiseLike = function (test) { | ||
var kewPromise = Q.defer() | ||
var qPromise = originalQ(10) | ||
var kewLikeObject = { | ||
promise: function () { | ||
return 'not a promise sucka!' | ||
}, | ||
then: function (fn) { | ||
fn('like a promise, brah!') | ||
} | ||
} | ||
test.equal(Q.isPromiseLike(kewPromise), true, 'A Kew promise is promise-like') | ||
test.equal(Q.isPromiseLike(qPromise), true, 'A Q promise is promise-like') | ||
test.equal(Q.isPromiseLike(kewLikeObject), true, 'A pretend promise is a promise-like') | ||
test.done() | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
50818
1102
0