Skip to content

Commit

Permalink
Remove support for new in bind
Browse files Browse the repository at this point in the history
Also normalize the use of thisp as a variable name.
  • Loading branch information
kriskowal committed Apr 30, 2012
1 parent 6be15b9 commit 71df2ef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 109 deletions.
32 changes: 7 additions & 25 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ exports.invoke = function (value, name) {
/**
* Applies the promised function in a future turn.
* @param object promise or immediate reference for target function
* @param context the context object (this) for the call
* @param thisp the `this` object for the call
* @param args array of application arguments
*/
var apply = exports.apply = dispatcher("apply");
Expand All @@ -803,13 +803,13 @@ var fapply = exports.fapply = dispatcher("fapply");
/**
* Calls the promised function in a future turn.
* @param object promise or immediate reference for target function
* @param context the context object (this) for the call
* @param thisp the `this` object for the call
* @param ...args array of application arguments
*/
exports.call = call;
function call(value, context) {
function call(value, thisp) {
var args = slice.call(arguments, 2);
return apply(value, context, args);
return apply(value, thisp, args);
}

/**
Expand All @@ -827,33 +827,15 @@ function fcall(value) {
* Binds the promised function, transforming return values into a fulfilled
* promise and thrown errors into a rejected one.
* @param object promise or immediate reference for target function
* @param context the context object (this) for the call
* @param thisp the `this` object for the call
* @param ...args array of application arguments
*/
exports.bind = bind;
function bind(value, context) {
function bind(value, thisp) {
var args = slice.call(arguments, 2);

return function bound() {
var allArgs = args.concat(slice.call(arguments));

if (this instanceof bound) {
var F = function () { };
F.prototype = value.prototype;
var self = new F();

var result = apply(value, self, allArgs);

return result.then(function (fulfilledValue) {
// if Object.isObject(fulfilledValue)
if (Object(fulfilledValue) === fulfilledValue) {
return fulfilledValue;
}
return self;
});
} else {
return apply(value, context, allArgs);
}
return apply(value, thisp, allArgs);
};
}

Expand Down
84 changes: 0 additions & 84 deletions test/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,90 +112,6 @@ exports['test uses existing context if none given'] = function (ASSERT, done) {
.fin(done);
};

exports['test invoking with new'] = function (ASSERT, done) {
function Point(x, y) {
this.x = x;
this.y = y;
}

var BoundPoint = Q.bind(Point, null, 1);

(new BoundPoint(2))
.then(function (point) {
ASSERT.deepEqual(point, { x: 1, y: 2 }, "fulfilled with constructed");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
}).
fin(done);
};

exports['test returns correctly when new\'ed'] = function (ASSERT, done) {
var returned = {};

var bound = Q.bind(function () { return returned; });

(new bound())
.then(function (val) {
ASSERT.strictEqual(val, returned, "fulfilled with correct value");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
};

exports['test throwing when new\'ed'] = function (ASSERT, done) {
var throwMe = new Error("boo!");

var bound = Q.bind(function () {
throw throwMe;
});

(new bound())
.then(function () {
ASSERT.ok(false, "should not be fulfilled");
})
.fail(function (reason) {
ASSERT.strictEqual(reason, throwMe, "rejected with correct reason");
})
.fin(done);
};

exports['test returns only objects when new\'ed'] = function (ASSERT, done) {
var bound = Q.bind(function () {
this.x = 1;

return 5;
});

(new bound())
.then(function (val) {
ASSERT.deepEqual(val, { x: 1 }, "fulfilled with object not primitive");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
};

exports['test returns only objects when new\'ed 2'] = function (ASSERT, done) {
var bound = Q.bind(function () {
this.x = 1;

return Q.resolve(5);
});

(new bound())
.then(function (val) {
ASSERT.deepEqual(val, { x: 1 }, "fulfilled with object not primitive");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
};

if (module == require.main) {
require('test').run(exports);
}

0 comments on commit 71df2ef

Please sign in to comment.