-
Notifications
You must be signed in to change notification settings - Fork 31
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
Allow promisifying methods (not just functions) #27
Comments
You should be able to pass a thisArg as the parameter to promisify. e.g. promisify(o.method, o); |
I know, this is what I meant by "binds the receiver explicitly". Whenever you use the function returned by promisify, What I'm doing is something like this:
|
Ah, I see. Do you think this is a good use case for a promisify library? Would it not be simpler to just return a promise? |
I meant to use it on methods that belong to libraries, which I did not write. Seems like a pretty good use case to me. (So |
I'm unconvinced of the use case for this. If you're modifying the prototype of a library, I'm not sure why you wouldn't do something like: LibraryClass.prototype.method_promise = function (...args) {
return new Promise((resolve, reject) => {
args.push((err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
this.method(...args);
});
}; or even: LibraryClass.prototype.method_promise = function (...args) {
if (!this._method_promise) {
this._method_promise = promisify(this.method, this);
}
return this._method_promise(...args);
}; But if you want to submit a PR, I'd be happy to merge it :) |
By the same token, you can always promisify a regular function by doing: fun promisified(...args) {
return new Promise(function (resolve, reject) {
libFunction(...args, function(err, data) {
if (err == undefined) resolve(data)
else reject(err)
})
})
} Isn't the whole point of the library to do this as a one-liner, |
As I say, if you want to submit a good PR I'd be happy to merge it in. |
I'll do it. A question regarding the interface however: my preference would be to drop the support for binding (the promisify(o.method).bind(o)
// or equivalently
promisify(o.method.bind(o)) Is that okay? |
@norswap I think you can just do |
Change this line to This must solve problem. |
promisify
binds the receiver explicitly, but what if we would like to call the promisified function with different receivers?As a workaround, I use this function:
But maybe there should be built-in support for this?
The text was updated successfully, but these errors were encountered: