From e929ad6dce052b6172ef5985d7f6ecb89f13e8df Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Wed, 26 Mar 2014 10:40:42 -0400 Subject: [PATCH 1/2] Change the type signature on Q.resolve, and add Promise.prototype.delay --- kew.js | 33 ++++++++++++++++++++++++++++----- package.json | 2 +- test/chain.js | 14 +++++++++++++- test/static.js | 4 +++- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/kew.js b/kew.js index 53f226c..e146f53 100644 --- a/kew.js +++ b/kew.js @@ -64,7 +64,7 @@ Promise.prototype.getContext = function () { /** * Resolve this promise with a specified value * - * @param {*} data + * @param {*=} data */ Promise.prototype.resolve = function (data) { if (this._error || this._hasData) throw new Error("Unable to resolve or reject the same promise twice") @@ -613,11 +613,23 @@ function defer() { /** * Return a promise which will wait a specified number of ms to resolve * - * @param {number} delayMs - * @param {*} returnVal - * @return {!Promise} returns returnVal + * @param {*} delayMsOrVal A delay (in ms) if this takes one argument, or ther + * return value if it takes two. + * @param {number=} opt_delayMs + * @return {!Promise} */ -function delay(delayMs, returnVal) { +function delay(delayMsOrVal, opt_delayMs) { + var returnVal = undefined + var delayMs = delayMsOrVal + if (typeof opt_delayMs != 'undefined') { + delayMs = opt_delayMs + returnVal = delayMsOrVal + } + + if (typeof delayMs != 'number') { + throw new Error('Bad delay value ' + delayMs) + } + var defer = new Promise() setTimeout(function onDelay() { defer.resolve(returnVal) @@ -625,6 +637,17 @@ function delay(delayMs, returnVal) { return defer } +/** + * Returns a promise that has the same result as `this`, but fulfilled + * after at least ms milliseconds + * @param {number} ms + */ +Promise.prototype.delay = function (ms) { + return this.then(function (val) { + return delay(val, ms) + }) +} + /** * Return a promise which will evaluate the function fn in a future turn with * the provided args diff --git a/package.json b/package.json index e68c9b9..b4c2455 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "devDependencies": { "q": "0.9.7", "nodeunit": "0.8.1", - "closure-npc": "0.0.12" + "closure-npc": "0.1.3" }, "scripts": { "test": "nodeunit test && closure-npc ./test/closure_test.js --jscomp_error=checkTypes" diff --git a/test/chain.js b/test/chain.js index a651af9..1a439a3 100644 --- a/test/chain.js +++ b/test/chain.js @@ -394,7 +394,7 @@ exports.testTimeout = function(test) { } exports.testNotTimeout = function(test) { - var promise = Q.delay(40, 'expected data').timeout(45, 'Timeout message') + var promise = Q.delay('expected data', 40).timeout(45, 'Timeout message') promise.then(function (data) { test.equals('expected data', data, 'The data should be the data from the original promise') }) @@ -414,3 +414,15 @@ exports.testNotTimeoutButReject = function(test) { }) .fin(test.done) } + +exports.testDelay = function (test) { + var timePassed = false + setTimeout(function () { + timePassed = true + }, 10) + Q.resolve('expected').delay(20).then(function (result) { + test.equal('expected', result) + test.ok(timePassed) + test.done() + }) +} diff --git a/test/static.js b/test/static.js index 84c9dea..eda2bed 100644 --- a/test/static.js +++ b/test/static.js @@ -163,7 +163,9 @@ exports.testDelay = function (test) { var startTime = Date.now() Q.resolve(val) - .then(Q.delay.bind(Q, 1000)) + .then(function (v) { + return Q.delay(v, 1000) + }) .then(function (returnVal) { test.equal(returnVal, val, "Val should be passed through") test.equal(Date.now() - startTime >= 1000, true, "Should have waited a second") From edff62ad2fe0f38251303bc4f2829ab015ae68e0 Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Wed, 26 Mar 2014 14:33:04 -0400 Subject: [PATCH 2/2] Update docs and package number --- README.md | 9 +++++++++ package.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac3d058..d1fd9ea 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,15 @@ getUrlContent(url1) }) ``` +If two arguments are passed, the first will be used as the return value, and the +second will be the delay in milliseconds. + +```javascript +Q.delay(obj, 20).then(function (result) { + console.log(result) // logs `obj` after 20ms +}) +``` + ### `.fcall()` for delaying a function invocation until the next tick: ```javascript // Assume someFn() is a synchronous 2 argument function you want to delay. diff --git a/package.json b/package.json index b4c2455..a80f133 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "kew", "description": "a lightweight promise library for node", - "version": "0.3.4", + "version": "0.4.0", "homepage": "https://github.com/Obvious/kew", "authors": [ "Jeremy Stanley (https://github.com/azulus)",