Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Allow nfbind bound function to handle missing args #753

Open
wants to merge 2 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- Q.any gives an error message from the last rejected promise
- Throw if callback supplied to "finally" is invalid (@grahamrhay)
- Allow the function created with nfbind to handle missing arguments

## 1.4.1

Expand Down
12 changes: 11 additions & 1 deletion q.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ function uncurryThis(f) {
// http://jsperf.com/uncurrythis

var array_slice = uncurryThis(Array.prototype.slice);
var array_concat = uncurryThis(Array.prototype.concat);

var array_reduce = uncurryThis(
Array.prototype.reduce || function (callback, basis) {
Expand Down Expand Up @@ -345,6 +346,14 @@ function isObject(value) {
return value === Object(value);
}

// Pad an array value with nulls to the specified length. Longer arrays are left alone.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't do what it says. It pads the array with holes, not nulls.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, you are right; I will change the comment.

function padArrayTo(array, length) {
if (array.length < length) {
array.length = length;
}
return array;
}

// generator related shims

// FIXME: Remove this function once ES6 generators are in SpiderMonkey.
Expand Down Expand Up @@ -1921,8 +1930,9 @@ Q.denodeify = function (callback /*...args*/) {
throw new Error("Q can't wrap an undefined function");
}
var baseArgs = array_slice(arguments, 1);
var expectLength = callback.length - baseArgs.length - 1;
return function () {
var nodeArgs = baseArgs.concat(array_slice(arguments));
var nodeArgs = baseArgs.concat(padArrayTo(array_slice(arguments), expectLength));
var deferred = defer();
nodeArgs.push(deferred.makeNodeResolver());
Q(callback).fapply(nodeArgs).fail(deferred.reject);
Expand Down
9 changes: 9 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,15 @@ describe("node support", function () {
});
});

it("allows bound function to support missing arguments", function () {
return Q.nfbind(function (a, b, c, d, callback) {
callback(null, !d);
}).call({}, 1, 2, 3)
.then(function (nod) {
expect(nod).toBe(true);
});
});

});

describe("nbind", function () {
Expand Down