Skip to content

Commit

Permalink
Merge pull request #35 from Obvious/nick-compiler
Browse files Browse the repository at this point in the history
Change the type signature on Q.resolve, and add Promise.prototype.delay
  • Loading branch information
nicks committed Mar 26, 2014
2 parents d8da796 + edff62a commit 2533f02
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 28 additions & 5 deletions kew.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -613,18 +613,41 @@ 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)
}, delayMs)
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
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (https://github.com/azulus)",
Expand All @@ -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"
Expand Down
14 changes: 13 additions & 1 deletion test/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand All @@ -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()
})
}
4 changes: 3 additions & 1 deletion test/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 2533f02

Please sign in to comment.