Comparing version 0.3.1 to 0.3.2
34
kew.js
@@ -207,2 +207,36 @@ /** @typedef {function(?, ?)} */ | ||
/** | ||
* Return a new promise that behaves the same as the current promise except | ||
* that it will be rejected if the current promise does not get fulfilled | ||
* after a certain amount of time. | ||
* | ||
* @param {number} timeoutMs The timeout threshold in msec | ||
* @param {string=} timeoutMsg error message | ||
* @returns a new promise with timeout | ||
*/ | ||
Promise.prototype.timeout = function (timeoutMs, timeoutMsg) { | ||
var deferred = new Promise() | ||
var isTimeout = false | ||
var timeout = setTimeout(function() { | ||
deferred.reject(new Error(timeoutMsg || 'Promise timeout after ' + timeoutMs + ' ms.')) | ||
isTimeout = true | ||
}, timeoutMs) | ||
this.then(function (data) { | ||
if (!isTimeout) { | ||
clearTimeout(timeout) | ||
deferred.resolve(data) | ||
} | ||
}, | ||
function (err) { | ||
if (!isTimeout) { | ||
clearTimeout(timeout) | ||
deferred.reject(err) | ||
} | ||
}) | ||
return deferred.promise | ||
} | ||
/** | ||
* Attempt to resolve this promise with the specified input | ||
@@ -209,0 +243,0 @@ * |
{ | ||
"name": "kew" | ||
, "description": "a lightweight promise library for node" | ||
, "version": "0.3.1" | ||
, "version": "0.3.2" | ||
, "homepage": "https://github.com/Obvious/kew" | ||
@@ -6,0 +6,0 @@ , "authors": [ |
@@ -379,1 +379,34 @@ var Q = require('../kew') | ||
} | ||
exports.testTimeout = function(test) { | ||
var promise = Q.delay(50).timeout(45, 'Timeout message') | ||
promise.then(function () { | ||
test.fail('The promise is supposed to be timeout') | ||
}) | ||
.fail(function (e) { | ||
test.equals('Timeout message', e.message, 'The error message should be the one passed into timeout()') | ||
}) | ||
.fin(test.done) | ||
} | ||
exports.testNotTimeout = function(test) { | ||
var promise = Q.delay(40, 'expected data').timeout(45, 'Timeout message') | ||
promise.then(function (data) { | ||
test.equals('expected data', data, 'The data should be the data from the original promise') | ||
}) | ||
.fail(function (e) { | ||
test.fail('The promise is supposed to be resolved before the timeout') | ||
}) | ||
.fin(test.done) | ||
} | ||
exports.testNotTimeoutButReject = function(test) { | ||
var promise = Q.delay(40).then(function() {throw new Error('Reject message')}).timeout(45, 'Timeout message') | ||
promise.then(function (data) { | ||
test.fail('The promise is supposed to be rejected') | ||
}) | ||
.fail(function (e) { | ||
test.equals('Reject message', e.message, 'The error message should be from the original promise') | ||
}) | ||
.fin(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
61445
1406
0