Comparing version 0.6.0 to 0.7.0
50
kew.js
@@ -31,3 +31,3 @@ | ||
if (typeof process !== 'undefined') { | ||
if (typeof process !== 'undefined' && typeof process.nextTick === 'function') { | ||
nextTick = process.nextTick | ||
@@ -804,3 +804,3 @@ } | ||
args.splice(1, 0, undefined) | ||
return bindPromise.apply(undefined, args)() | ||
return ncall.apply(undefined, args) | ||
} | ||
@@ -810,2 +810,15 @@ | ||
/** | ||
* Like `nfcall`, but permits passing a `this` context for the call. | ||
* | ||
* @param {function(...)} fn | ||
* @param {Object} scope | ||
* @param {...*} var_args | ||
* @return {!Promise} | ||
*/ | ||
function ncall(fn, scope, var_args) { | ||
return bindPromise.apply(null, arguments)() | ||
} | ||
/** | ||
* Binds a function to a scope with an optional number of curried arguments. Attaches | ||
@@ -833,18 +846,19 @@ * a node style callback as the last argument and returns a promise | ||
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, | ||
defer: defer, | ||
delay: delay, | ||
fcall: fcall, | ||
isPromise: isPromise, | ||
isPromiseLike: isPromiseLike, | ||
ncall: ncall, | ||
nfcall: nfcall, | ||
resolve: resolve, | ||
reject: reject, | ||
spread: spread, | ||
stats: stats, | ||
allSettled: allSettled, | ||
Promise: Promise, | ||
getNextTickFunction: getNextTickFunction, | ||
setNextTickFunction: setNextTickFunction, | ||
} |
{ | ||
"name": "kew", | ||
"description": "a lightweight promise library for node", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"homepage": "https://github.com/Medium/kew", | ||
@@ -11,2 +11,3 @@ "authors": [ | ||
], | ||
"license": "Apache-2.0", | ||
"contributors": [], | ||
@@ -13,0 +14,0 @@ "keywords": [ |
@@ -257,3 +257,3 @@ kew: a lightweight (and super fast) promise/deferred framework for node.js | ||
### `.nfcall()` for Node.js callbacks | ||
### `.ncall()` and `.nfcall()` for Node.js callbacks | ||
@@ -272,2 +272,11 @@ ``Q.nfcall()`` can be used to convert node-style callbacks into promises: | ||
If your Node-style callback needs a `this` context, you can use `Q.ncall`: | ||
```js | ||
Q.ncall(redis.del, redis, 'my-key') | ||
.then(function () { return true }) | ||
.fail(function () { return false }) | ||
``` | ||
### `.spread()` for arrays of promises | ||
@@ -274,0 +283,0 @@ |
@@ -36,1 +36,25 @@ var Q = require('../kew') | ||
} | ||
exports.testAllSynchronization1 = function (test) { | ||
var order = [] | ||
Q.resolve(true) | ||
.then(function () { | ||
var promiseA = Q.fcall(function () { | ||
order.push('a') | ||
}) | ||
var promiseB = Q.fcall(function () { | ||
order.push('b') | ||
}) | ||
test.deepEqual([], order) | ||
var promiseAB = Q.all([promiseA, promiseB]) | ||
test.deepEqual([], order) | ||
return [promiseA, promiseB] | ||
}) | ||
.then(function (results) { | ||
test.deepEqual(['a', 'b'], order) | ||
test.done() | ||
}) | ||
} |
@@ -322,2 +322,19 @@ var Q = require('../kew') | ||
exports.testNcall = 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.ncall(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 | ||
@@ -324,0 +341,0 @@ exports.testBindPromise = function (test) { |
77070
1924
321