Skip to content

Commit

Permalink
Fix the equivalent of kriskowal#134 but for npost.
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Nov 4, 2012
1 parent 73cc7ed commit 61a290d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 9 additions & 4 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,12 @@ function nbind(callback /* thisp, ...args*/) {
*/
exports.npost = npost;
function npost(object, name, args) {
return napply(object[name], object, args);
var nodeArgs = array_slice(args);
var deferred = defer();
nodeArgs.push(deferred.makeNodeResolver());

post(object, name, nodeArgs).fail(deferred.reject);
return deferred.promise;
}

/**
Expand All @@ -1588,11 +1593,11 @@ function npost(object, name, args) {
*/
exports.ninvoke = ninvoke;
function ninvoke(object, name /*, ...args*/) {
var args = array_slice(arguments, 2);
var nodeArgs = array_slice(arguments, 2);
var deferred = defer();
args.push(deferred.makeNodeResolver());
nodeArgs.push(deferred.makeNodeResolver());

post(object, name, args).fail(deferred.reject);
post(object, name, nodeArgs).fail(deferred.reject);
return deferred.promise;
}

Expand Down
17 changes: 17 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,23 @@ describe("node support", function () {
});
});

it("rejects with thrown error", function () {
return Q.npost(obj, "errorThrower", [1, 2, 3])
.then(function (sum) {
expect(true).toBe(false);
}, function (_exception) {
expect(_exception).toBe(exception);
});
});

it("works on promises for objects with Node methods", function () {
return Q.resolve(obj)
.npost("method", [1, 2, 3])
.then(function (sum) {
expect(sum).toEqual(6);
});
});

});

describe("ninvoke", function (done) {
Expand Down

0 comments on commit 61a290d

Please sign in to comment.