Skip to content

Commit

Permalink
Add Q.call, like nfcall but accepts a thisArg
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehg committed Aug 24, 2015
1 parent 5aa9705 commit 6598ada
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
48 changes: 31 additions & 17 deletions kew.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,20 @@ function nfcall(fn, var_args) {
// Insert an undefined argument for scope and let bindPromise() do the work.
var args = Array.prototype.slice.call(arguments, 0)
args.splice(1, 0, undefined)
return bindPromise.apply(undefined, args)()
return call.apply(undefined, args)
}


/**
* Like `nfcall`, but permits passing a `this` context for the call.
*
* @param {function(...)} fn
* @param {Object} scope
* @param {...*} var_args
* @return {!Promise}
*/
function call(fn, scope, var_args) {
return bindPromise.apply(null, arguments)()
}


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

module.exports = {
all: all
, bindPromise: bindPromise
, defer: defer
, delay: delay
, fcall: fcall
, isPromise: isPromise
, isPromiseLike: isPromiseLike
, nfcall: nfcall
, resolve: resolve
, reject: reject
, spread: spread
, stats: stats
, allSettled: allSettled
, Promise: Promise
, getNextTickFunction: getNextTickFunction
, setNextTickFunction: setNextTickFunction
all: all,
bindPromise: bindPromise,
call: call,
defer: defer,
delay: delay,
fcall: fcall,
isPromise: isPromise,
isPromiseLike: isPromiseLike,
nfcall: nfcall,
resolve: resolve,
reject: reject,
spread: spread,
stats: stats,
allSettled: allSettled,
Promise: Promise,
getNextTickFunction: getNextTickFunction,
setNextTickFunction: setNextTickFunction,
}
17 changes: 17 additions & 0 deletions test/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ exports.testNFcallErrorSync = function (test) {
})
}

exports.testCall = function (test) {
function TwoAdder() {
this.a = 2
}
TwoAdder.prototype.add = function (num, callback) {
setTimeout(function () {
callback(null, this.a + num)
}.bind(this), 10)
}
var adder = new TwoAdder()
Q.call(adder.add, adder, 3)
.then(function (val) {
test.equal(val, 5, "Val should be 2 + 3")
test.done()
})
}

// test binding a callback function with a promise
exports.testBindPromise = function (test) {
var adder = function (a, b, callback) {
Expand Down

0 comments on commit 6598ada

Please sign in to comment.