Skip to content

Feature request: Promise support #263

Description

@dan-perron

Functions that take a callback should return a promise if no callback is provided.

Activity

  1. hypesystem commented on Jul 20, 2016

    @hypesystem
    Collaborator

    It should be possible to promisify the exposed interface, and it will be even easier with the upcoming v1 :)

    i don't think we will do this for the v0 branch, but we might consider it on the v1 branch.

    Personally I think letting the user promisify the interface is the superior option, as this is easy and limits the scope of this lib. I am open to discussion on this point, though.

  2. dan-perron commented on Jul 20, 2016

    @dan-perron
    Author

    Is there a timeline for V1?

    I'd consider creating a patch if you'd be interested in the feature (and it's going to be released in the near future)

  3. hypesystem commented on Jul 20, 2016

    @hypesystem
    Collaborator

    The timeline is "when we've checked most or all things off our list" #238 -- my best guess is quite soon after my summer holiday is up. Early to mid september, probably.

  4. dan-perron commented on Jul 20, 2016

    @dan-perron
    Author

    Fair enough. Starting to chew on this a little bit.

    Can you explain the rationale behind using nextTick for calling callbacks in many places rather than calling them directly?

    Let me know if there's a better place to post a question like this.

  5. hypesystem commented on Jul 20, 2016

    @hypesystem
    Collaborator

    A function that takes a callback should never call that callback before yielding to the event loop. Using http or fs or similar usually does this, but for consistency it may slmetimes be necessary to do this explicitly.

    It is commonly expected that code like this will always print "b" before "a":

    something(function(error) {
        console.log("a");
    });
    
    console.log("b");

    Calling the callback directly in the cases you refer to would result in "a" being printed before "b".

  6. eladnava commented on Jul 20, 2016

    @eladnava
    Collaborator

    This post also covers it nicely:
    https://howtonode.org/understanding-process-next-tick

    Consider this example taken from the post:

    var client = net.connect(8124, function() { 
        console.log('client connected');
        client.write('world!\r\n');
    });

    In the above case, if for some reason, net.connect() invoked the callback directly without process.nextTick, the callback would be called immediately, and hence the client variable will not be initialized when the it's accessed by the callback to write to the client!

  7. eladnava commented on Jul 20, 2016

    @eladnava
    Collaborator

    And for what it's worth, I'd love for promises to be supported out of the box in v1. I personally yield promises in generator functions in most of my projects and I hate having to promisify or thunkify external library functions manually. I love it when libraries provide either a callback or promise interface (if a callback is omitted).

  8. hypesystem commented on Jul 20, 2016

    @hypesystem
    Collaborator

    Alright :-) let's do it

  9. dan-perron commented on Jul 21, 2016

    @dan-perron
    Author

    For your consideration #264

  10. eladnava commented on Oct 3, 2016

    @eladnava
    Collaborator

    Closing this as PR was merged successfully. 😄

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions