async-listener icon indicating copy to clipboard operation
async-listener copied to clipboard

Using promisify on setTimeout causes it to fail

Open ATGardner opened this issue 7 years ago • 1 comments
trafficstars

I have created a simple test (https://gist.github.com/ATGardner/7c34a90b01ed0bf513ccd6911b4372f3) which shows that when I try to use a promisified setTimeout call, it fails with

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

When using promisify(setTimeout) on the unwrapped, original, setTimeout function, it works just fine.

I understand this is because the original function has a util.promisify.custom field which contains the custom promisified version of it. But the wrapped one does not, and promisify can't automatically wrap a function with a callback as the 1st argument. I guess the solution would be to also create and wrap the util.promisify.custom field of setTimeout in async-listener/index.js (and maybe a more general solution, to other functions as well)

ATGardner avatar Aug 21 '18 05:08 ATGardner

Thanks for taking the time to investigate this issue.

Since Node.js version 8, Async Hooks have been part of Node core, which does more or less the same thing as this polyfill module. The util.promisify function was also added in Node 8. If you're using Node 8 or higher I would recommend using Async Hooks instead of this module.

I know this is not an solution to the problem you encountered and it probably is possible fix it in this module. But I don't personally have time to look into it. So I would recommend that you use Async Hooks instead if at all possible. You are of course also more than welcome to open a PR with a fix your self. Then I'm sure my self or one of the other maintainers will review it.

watson avatar Aug 26 '18 19:08 watson