-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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 context option to utils.promisify() for method binding #13338
Comments
@wesbos I believe all Stripe methods return promises if callback is not provided https://github.com/stripe/stripe-node#using-promises But idea of context for |
Oh - I didn't know that! Awesome. But yes - I've needed this for many other libs that don't offer promises. |
@wesbos how do you imagine this should work in the following case? var o1 = {a: true};
var o2 = {a: false};
function foo(cb) { cb(null, this.a); }
o2.f = util.promisify(foo, o1);
o2.f().then(console.log); |
The PR implementing this got negative response so I guess we should close both the PR and this issue. cc @nodejs/collaborators objections? |
Hi, I don't think we should mess with contexts, the user can reassign the function to the object and context will work (promisify has tests for this).
|
Closing due to lack of support, see also #13440. |
You can promisify all object methods without object modification using util.promisify and ES6 Proxy. |
(Just a note, Proxies are cool but pretty slow.) |
I think |
Be careful with this, doing something like:
while other packages use |
Applying the native way as described in https://github.com/doasync/doasync, I guess we could do this. const func2 = util.promisify(func)
const ret = await func2.apply(context, [arg1, arg2, ...]) |
For future generations reading this:
is side-effect free and still one line. |
@roychri for posterity - we offer |
The new
utils.promisify()
will promisify methods, but they lose theirthis
context. Many libs require their methods to be bound.Here is an example I'm working on with the offical Stripe package.
If I want to promisify this:
Ideally I would do this:
I've been using the ES6-promisify package for a while now, and it has this option. The Bluebird promisify method also has an option for this.
The text was updated successfully, but these errors were encountered: