Skip to content
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

Expose reference to the Symbol(customPromisifyArgs) #14007

Closed
jcready opened this issue Jun 30, 2017 · 2 comments
Closed

Expose reference to the Symbol(customPromisifyArgs) #14007

jcready opened this issue Jun 30, 2017 · 2 comments
Labels
question Issues that look for answers. util Issues and PRs related to the built-in util module.

Comments

@jcready
Copy link

jcready commented Jun 30, 2017

Since node 8 and the introduction of util.promisify() many internal node modules have started using these two Symbols to provide hints to util.promisify() as to how the function should be promisified. However, only Symbol(util.promisify.custom) is readily available:

const customPromisifySymbol = require('util').promisify.custom

The second one Symbol(customPromisifyArgs) is a bit more difficult to get a reference to, but it can be done:

const customArgsSymbol = Object.getOwnPropertySymbols(require('fs').read)
  .find((s) => String(s) === 'Symbol(customPromisifyArgs)')

Would it be possible to also expose a reference to the latter in a more convenient way, maybe as require('util').promisify.args? Is there a reason Symbol(customPromisifyArgs) isn't exposed the same way as Symbol(util.promisify.custom)?

@addaleax addaleax added the util Issues and PRs related to the built-in util module. label Jun 30, 2017
@TimothyGu
Copy link
Member

TimothyGu commented Jun 30, 2017

From the discussion that landed util.promisify in core, we exposed the more generic custom symbol while hiding customPromisifyArgs because we wanted to gauge if/how much the latter could be helpful to userland, especially since the latter could be implemented in terms of the former. Plus, the semantics of Symbol(customPromisifyArgs) is not as clear and undebatable as Symbol(util.promisify.custom).

And...

const customArgsSymbol = Object.getOwnPropertySymbols(require('fs').read)
  .find((s) => String(s) === 'Symbol(customPromisifyArgs)')

Please don't use this in production code. Unexported things are not exported for a reason :)

@jcready
Copy link
Author

jcready commented Jun 30, 2017

Please don't use this in production code. Unexported things are not exported for a reason :)

Ha! You should've used an internal WeakMap if you didn't want users grabbing the symbols via reflection :feelsgood:

const internalPromisifyArgsMap = new WeakMap()
internalPromisifyArgsMap.set(require('fs').read, [ 'bytesRead', 'buffer' ])

Anyways, thanks that does make a lot of sense.

@jcready jcready closed this as completed Jun 30, 2017
@addaleax addaleax added the question Issues that look for answers. label Jun 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

3 participants