Skip to content

Instantly share code, notes, and snippets.

@terinjokes
Last active December 13, 2015 19:48
Show Gist options
  • Save terinjokes/4965516 to your computer and use it in GitHub Desktop.
Save terinjokes/4965516 to your computer and use it in GitHub Desktop.
console.log(Q);
/*
>> function Q(value) {
>> return resolve(value);
>> }
*/
var promise = _.extend({
"ref": function() {}
}, Q);
console.log(promise);
console.log(promise.ref);
/*
* Expected:
>> function Q(value) {
>> return resolve(value);
>> }
>>
>> function() {}
*
* Actual:
>> Object {ref: function, longStackJumpLimit: 1, nextTick: function, defer: function, promise: function…}
>>
>> function() {}
*/
/*
* While I have your attention, while debugging this I noticed something else
* fairly interesting: the new callback and thisArgs support in extend/assign,
* and merge causes issues with source objects like Q that are also functions.
*/
var promise = _.extend({}, {
"ref": function() {}
}, Q);
console.log(promise);
/*
* Expected:
>> function Q(value) {
>> return resolve(value);
>> }
*
* Actual:
>> Object {ref: makePromise}
*/
@jdalton
Copy link

jdalton commented Feb 16, 2013

Your _.extend issue was addressed in c280347.

@terinjokes
Copy link
Author

Having seen issue thread for lodash/lodash#184 in my email, I figured as much. Now that I've upgraded (back) to edge, the following code works:

var promise = _.extend(_.bind(Q), Q, {
  "ref": function() {}
});

Switching the order of the last two arguments does not work: I suppose because Lo-Dash has no way of determining if the function provided as the last argument (or second-to-last) is a callback or sourceN. I'm not sure how popular this is, though I believe it warrants a mention in the documentation.

@jdalton
Copy link

jdalton commented Feb 17, 2013

The behavior of _.assign is mentioned in the docs, with the fix in edge the callback behavior will not be used if callback is the first or second arg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment