-
Notifications
You must be signed in to change notification settings - Fork 30k
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
util.promisify does not work as expected with object methods #30344
Comments
You can use await listen.call(server, 9123); or better use |
Regarding your proposed solution: That would mean that you would have to create a new promisified function for each function and for each instance. I think @lpinca's suggestion is generally preferrable over that. |
Please note that I'm not asking for help to solve my problem, I already did by writing my own I'm reporting that, imho, |
Why not? You are promisifying a function on const listen = server.listen;
listen(9123, callback); |
I agree with @lpinca, it is working as expected, that is just how JavaScript works.
You can do that, or you can just use util.promisify(server.listen.bind(server)) |
In case, I prefer this solution const server = net.createServer()
const listen = util.promisify(server.listen.bind(server))
await listen(9123) Don't you think this should be in the documentation? |
That seems to be exactly what I suggested 👍 |
Submitting in the same time :) |
It is just how JavaScript works, but since it is apparently causing confusion, I would be okay with briefly mentioning this in the documentation. |
Thank you |
You'd probably want something like this. const server = net.createServer()
server.listen(9123)
await events.once(server, 'listening') Otherwise, your app might hang if the |
Hi all
I'm having an issue with
util.promisify
, trying to apply it to an object method, as followoutput is
because in
net.js:1383
there iswhile
util.promisify
doso
this
at execution oflisten
function is no more its own instance, but a new context.Reading the documentation, this is not reported, so I'm opening this issue - that I suppose to be a bug.
In my opinion, a possible solution could be adding an optional arg
instance
toutil.promisify
, likethen, after checked that instance is an object and
instance[original]
exists, could beit could be called as
and works as aspected.
If you agree with this solution, I'd be glad to do that.
The text was updated successfully, but these errors were encountered: