Skip to content

Commit 0c98c0a

Browse files
committed
Add Q.call, like nfcall but accepts a thisArg
1 parent 5aa9705 commit 0c98c0a

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Q.fcall(someFn, arg1, arg2)
254254

255255
You can also use ``Q.fcall()`` with functions that return promises.
256256

257-
### `.nfcall()` for Node.js callbacks
257+
### `.ncall()` and `.nfcall()` for Node.js callbacks
258258

259259
``Q.nfcall()`` can be used to convert node-style callbacks into promises:
260260

@@ -268,6 +268,15 @@ Q.nfcall(fs.writeFile, '/tmp/myFile', 'content')
268268
})
269269
```
270270

271+
If your Node-style callback needs a `this` context, you can use `Q.ncall`:
272+
273+
```js
274+
Q.ncall(redis.del, redis, 'my-key')
275+
.then(function () { return true })
276+
.fail(function () { return false })
277+
```
278+
279+
271280
### `.spread()` for arrays of promises
272281

273282
``()`` can be used to convert node-style callbacks into promises:

kew.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,20 @@ function nfcall(fn, var_args) {
800800
// Insert an undefined argument for scope and let bindPromise() do the work.
801801
var args = Array.prototype.slice.call(arguments, 0)
802802
args.splice(1, 0, undefined)
803-
return bindPromise.apply(undefined, args)()
803+
return ncall.apply(undefined, args)
804+
}
805+
806+
807+
/**
808+
* Like `nfcall`, but permits passing a `this` context for the call.
809+
*
810+
* @param {function(...)} fn
811+
* @param {Object} scope
812+
* @param {...*} var_args
813+
* @return {!Promise}
814+
*/
815+
function ncall(fn, scope, var_args) {
816+
return bindPromise.apply(null, arguments)()
804817
}
805818

806819

@@ -827,20 +840,21 @@ function bindPromise(fn, scope, var_args) {
827840
}
828841

829842
module.exports = {
830-
all: all
831-
, bindPromise: bindPromise
832-
, defer: defer
833-
, delay: delay
834-
, fcall: fcall
835-
, isPromise: isPromise
836-
, isPromiseLike: isPromiseLike
837-
, nfcall: nfcall
838-
, resolve: resolve
839-
, reject: reject
840-
, spread: spread
841-
, stats: stats
842-
, allSettled: allSettled
843-
, Promise: Promise
844-
, getNextTickFunction: getNextTickFunction
845-
, setNextTickFunction: setNextTickFunction
843+
all: all,
844+
bindPromise: bindPromise,
845+
defer: defer,
846+
delay: delay,
847+
fcall: fcall,
848+
isPromise: isPromise,
849+
isPromiseLike: isPromiseLike,
850+
ncall: ncall,
851+
nfcall: nfcall,
852+
resolve: resolve,
853+
reject: reject,
854+
spread: spread,
855+
stats: stats,
856+
allSettled: allSettled,
857+
Promise: Promise,
858+
getNextTickFunction: getNextTickFunction,
859+
setNextTickFunction: setNextTickFunction,
846860
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "kew",
33
"description": "a lightweight promise library for node",
4-
"version": "0.6.0",
4+
"version": "0.7.0",
55
"homepage": "https://github.com/Medium/kew",
66
"authors": [
77
"Jeremy Stanley <[email protected]> (https://github.com/azulus)",

test/static.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,23 @@ exports.testNFcallErrorSync = function (test) {
319319
})
320320
}
321321

322+
exports.testNcall = function (test) {
323+
function TwoAdder() {
324+
this.a = 2
325+
}
326+
TwoAdder.prototype.add = function (num, callback) {
327+
setTimeout(function () {
328+
callback(null, this.a + num)
329+
}.bind(this), 10)
330+
}
331+
var adder = new TwoAdder()
332+
Q.ncall(adder.add, adder, 3)
333+
.then(function (val) {
334+
test.equal(val, 5, "Val should be 2 + 3")
335+
test.done()
336+
})
337+
}
338+
322339
// test binding a callback function with a promise
323340
exports.testBindPromise = function (test) {
324341
var adder = function (a, b, callback) {

0 commit comments

Comments
 (0)