Comparing version 0.3.4 to 0.4.0
45
kew.js
@@ -67,3 +67,3 @@ | ||
* | ||
* @param {*} data | ||
* @param {*=} data | ||
*/ | ||
@@ -617,7 +617,19 @@ Promise.prototype.resolve = function (data) { | ||
* | ||
* @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() | ||
@@ -631,2 +643,13 @@ setTimeout(function onDelay() { | ||
/** | ||
* 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 | ||
@@ -643,3 +666,7 @@ * the provided args | ||
process.nextTick(function onNextTick() { | ||
defer.resolve(fn.apply(undefined, rootArgs)) | ||
try { | ||
defer.resolve(fn.apply(undefined, rootArgs)) | ||
} catch (e) { | ||
defer.reject(e) | ||
} | ||
}) | ||
@@ -679,3 +706,7 @@ return defer | ||
var defer = new Promise() | ||
fn.apply(scope, rootArgs.concat(Array.prototype.slice.call(arguments, 0), defer.makeNodeResolver())) | ||
try { | ||
fn.apply(scope, rootArgs.concat(Array.prototype.slice.call(arguments, 0), defer.makeNodeResolver())) | ||
} catch (e) { | ||
defer.reject(e) | ||
} | ||
return defer | ||
@@ -682,0 +713,0 @@ } |
{ | ||
"name": "kew", | ||
"description": "a lightweight promise library for node", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/Obvious/kew", | ||
@@ -25,3 +25,3 @@ "authors": [ | ||
"nodeunit": "0.8.1", | ||
"closure-npc": "0.0.12" | ||
"closure-npc": "0.1.3" | ||
}, | ||
@@ -28,0 +28,0 @@ "scripts": { |
@@ -235,2 +235,11 @@ kew: a lightweight (and super fast) promise/deferred framework for node.js | ||
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: | ||
@@ -237,0 +246,0 @@ ```javascript |
@@ -397,3 +397,3 @@ var Q = require('../kew') | ||
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) { | ||
@@ -418,1 +418,13 @@ test.equals('expected data', data, 'The data should be the data from the original promise') | ||
} | ||
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() | ||
}) | ||
} |
@@ -166,3 +166,5 @@ var Q = require('../kew') | ||
Q.resolve(val) | ||
.then(Q.delay.bind(Q, 1000)) | ||
.then(function (v) { | ||
return Q.delay(v, 1000) | ||
}) | ||
.then(function (returnVal) { | ||
@@ -191,2 +193,19 @@ test.equal(returnVal, val, "Val should be passed through") | ||
// test fcall | ||
exports.testFcallError = function (test) { | ||
var error = function () { | ||
throw new Error('my error') | ||
} | ||
Q.fcall(error) | ||
.then(function (val) { | ||
test.fail('fcall should throw exception') | ||
}, function (err) { | ||
test.equal('my error', err.message) | ||
}) | ||
.then(function () { | ||
test.done() | ||
}) | ||
} | ||
// test fcall works when fn returns a promise | ||
@@ -240,2 +259,19 @@ exports.testFcallGivenPromise = function (test) { | ||
// test fcall | ||
exports.testNFcallErrorSync = function (test) { | ||
var error = function () { | ||
throw new Error('my error') | ||
} | ||
Q.nfcall(error) | ||
.then(function (val) { | ||
test.fail('nfcall should throw exception') | ||
}, function (err) { | ||
test.equal('my error', err.message) | ||
}) | ||
.then(function () { | ||
test.done() | ||
}) | ||
} | ||
// test binding a callback function with a promise | ||
@@ -242,0 +278,0 @@ exports.testBindPromise = function (test) { |
67784
1637
297