Skip to content

Consistent sync and async function names #5

Description

@jonathanong

Specifically all synchronous functions should be *Sync and all other functions should be async. Mixed usage would be deprecated, but not removed for backwards compatibility.

For example, we should deprecate crypto functions like crypto.randomBytes(length) in favor of crypto.randomBytesSync(length).

There is no request for additional functionality. PR welcomed?

Reference: nodejs/node-v0.x-archive#7030

Activity

  1. indutny commented on Nov 28, 2014

    @indutny
    Member

    Haha, let the discussion begin! :)

  2. calvinmetcalf commented on Nov 28, 2014

    @calvinmetcalf
    Contributor

    I believe it's just crypto that still has problems right? Some places where async/non-blocking could help

    • createDiffieHellman(length) this can take a very long time so a non blocking version of this would be helpful, as this is a constructor not a method it would require a bit more of an api tweak
    • diffieHellman computeSecret, and generateKeys
    • sign, verify, publicEncrypt, and privateDecrypt
    • async, non-streaming encryption/decreption, authenticated encryption methods are fundamentally incompatible with streaming because you are supposed to authenticate before doing stuff with the data, a encrypt method which took an algorithm, data, password or key/iv and for gcm aad and a tag on decryption could have a sync an async versions (maybe slip in some stronger key derivation).
    • non blocking hash stream? not sure if it would be much of a benefit.
  3. iefserge commented on Nov 28, 2014

    @iefserge

    Would be nice to be able to call any async function as sync like this:

    var sync = require('sync');
    var fs = require('fs');
    
    var stats = sync(fs.stat(file))

    This could also work for user defined functions.

  4. vkurchatkin commented on Nov 28, 2014

    @vkurchatkin
    Contributor
  5. nmn commented on Nov 28, 2014

    @nmn

    I think that while Node should be slowly cleaned up and made more consistent, it would be a mistake to change too much too soon in an overeagerness to improve things.

    Instead, I think the best way forward would be to embrace the existing API and extend it with new features from ES6. (It's in everyone's best interest if Node.js stays consistent with the standards)

    That said, renaming functions to make them more consistent with the general naming scheme is obviously a great idea. But let's not overhaul how sync and async functions work at the moment.

    There are various ways to easily wrap packages as Promises or for generators. That is the best approach for now, as even though callbacks can be painful, they are the lowest level code possible, and can be wrapped to be compatible with Promises, Observables, Generators, Thunks and Channels. That flexibility is worth something.

  6. mikeal commented on Nov 28, 2014

    @mikeal
    Contributor

    @iefserge that isn't actually possible. The best we could do would be

    var sync = require('sync');
    var fs = require('fs');
    
    var stats = sync(fs.stat)(file)
  7. mikeal commented on Nov 28, 2014

    @mikeal
    Contributor

    @Naman34 that is already happening above core, see: co and thunkify for instance.

    In general, if something is being accomplished well in the ecosystem then core should stay out of it.

  8. yoshuawuyts commented on Nov 28, 2014

    @yoshuawuyts

    @iefserge I'd argue that we probably need less synchronous functions, not more. Having a sync() function is extremely powerful, but ultimately I think it would tilt the platform in the wrong direction (e.g. away from being an async event machine).

    Actually I'd be interested in what would happen if all sync functions would be removed entirely. I've had the impression many of the sync functions have been added just for the hell of it. Not sure if possible though, haha, mostly an interesting thought experiment.

  9. mikeal commented on Nov 28, 2014

    @mikeal
    Contributor

    @yoshuawuyts the module system relies on a bunch of sync calls, so they can't be removed entirely.

  10. bnoordhuis commented on Nov 28, 2014

    @bnoordhuis
    Member

    createDiffieHellman(length) this can take a very long time so a non blocking version of this would be helpful, as this is a constructor not a method it would require a bit more of an api tweak
    diffieHellman computeSecret, and generateKeys
    sign, verify, publicEncrypt, and privateDecrypt

    Agreed. This seems uncontroversial.

    async, non-streaming encryption/decreption, authenticated encryption methods are fundamentally incompatible with streaming because you are supposed to authenticate before doing stuff with the data, a encrypt method which took an algorithm, data, password or key/iv and for gcm aad and a tag on decryption could have a sync an async versions (maybe slip in some stronger key derivation).

    I'm not sure what you mean here. That you need to buffer before you can encrypt/decrypt/verify?

  11. iefserge commented on Nov 28, 2014

    @iefserge

    @mikeal

    that isn't actually possible

    It's possible, but fs.stat needs to return some special async operation id value. And sync(id) blocks on that operation.
    But var stats = sync(fs.stat)(file) would work as well.

  12. vkurchatkin commented on Nov 28, 2014

    @vkurchatkin
    Contributor

    fs.stat needs to return some special async operation id value

    or simply a promise.

  13. darrenderidder commented on Nov 28, 2014

    @darrenderidder

    Promises, like the many other async abstractions that exist, belong in user modules.

  14. nmn commented on Nov 28, 2014

    @nmn

    In general, if something is being accomplished well in the ecosystem then core should stay out of it.
    @mikeal You basically said what I was trying to say in a concise way.

    @darrenderidder: +1 to what you said.

    I think sync function were added mostly for building command-line utilities. Let's not forget that node isn't ONLY for web servers.

  15. vkurchatkin commented on Nov 28, 2014

    @vkurchatkin
    Contributor

    Promises, like the many other async abstractions that exist, belong in user modules.

    Yes, and that is why they are a part of the language

  16. 56 remaining items

  17. added a commit that references this issue on Jul 18, 2024
  18. nodejs-github-bot commented on Jul 28, 2025

    @nodejs-github-bot
    Collaborator
  19. added a commit that references this issue on Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions