-
-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bind option #27
add bind option #27
Conversation
See the first commit. There I added accessor methods that delegate to the bound object. That seems like overkill, still it might be nice: var x = {
option: 'foo',
method: function(cb) {
setImmediate(() => cb(null, this.option));
}
}
var y = pify(x);
// With delegate accessors you could do:
y.option = 'bar';
y.method().then(...);
// Without delegate accessors you need to remember to set on x:
x.option = 'bar';
y.method().then(...); I removed that in the second commit (code is a lot smaller / cleaner without the feature) |
@@ -1,8 +1,8 @@ | |||
'use strict'; | |||
|
|||
var processFn = function (fn, P, opts) { | |||
var processFn = function (fn, P, opts, bind) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamestalmage Is there a reason not to use opts.bind
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. It caused the optimization test to fail (not sure why, but the optimization killers blog post does mention something about fn.bind()
.
Also, we are already calling fn.apply(someObj, args)
anyways. Adding a layer of indirection where this
doesn't end up being someObj
seems confusing.
@schnittstabil @SamVerschueren What do you think about handling getter/setters too 32e5fe7? It kinda makes sense to me. |
const obj = { | ||
x: 'foo', | ||
y: function (cb) { | ||
setImmediate(() => cb(null, this.x)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if it's preferred, but setImmediate
will pass on any extra arguments, so this could be written like this:
setImmediate(cb, null, this.x);
I can update the PR, but do we want the property accessors when binding, or not? |
@jamestalmage I'm not sure. Are there any potential downsides to it? What does bluebird.promisify do? I'm just afraid of it having some pitfalls and requiring yet another major release at some point later. |
I don't think bluebird binds anything but methods |
In my personal vision, Based on this idea:
Therefore I don't see any downsides providing 1. |
@schnittstabil - That's basically what the first commit does (and the second commit undoes). It's not a complete proxy object (the property has to exist at the time of promisification). |
@jamestalmage I wanted to show, that I don't believe, that your suggestion have to be reverted in the future. |
Yeah. I had that thought recently and forgot to mention it. If everybody is 👍 on the feature, I will revert the second commit and just do all enumerable properties (or do we need non enumerable too?) |
👍
Nah. They're non-enumerable for a reason. |
@schnittstabil I really like this idea. Mind opening an issue so we can consider doing that in the future? |
This reverts commit 4019aaa.
Hmmm: class Foo {
bar () {}
}
new Foo().propertyIsEnumerable('bar');
// => false |
@jamestalmage Agh. I forgot that ES2015 class members are non-enumerable. I guess we have to get those too then. |
Because of #36 (comment), I believe the best way to deal with this is a dedicated // instead of
const y = pify(x, {bind: true});
// we can use a bind library
const y = pify(bindAll(x)); I believe the semantics would be more clear. Furthermore, the loss of performance seems to be negligible, because we have to bind all(relevant!?) functions, not only the functions we want to promisify. |
By default, it binds functions to the original object.
If
opts.bind
isfalse
. It does not bind to the original object, and copies over all non-function properties.Closes #26.