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

Feature request: Promise support #263

Closed
dan-perron opened this issue Jul 20, 2016 · 10 comments
Closed

Feature request: Promise support #263

dan-perron opened this issue Jul 20, 2016 · 10 comments

Comments

@dan-perron
Copy link

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

@hypesystem
Copy link
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.

@dan-perron
Copy link
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)

@hypesystem
Copy link
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.

@dan-perron
Copy link
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.

@hypesystem
Copy link
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".

@eladnava
Copy link
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!

@eladnava
Copy link
Collaborator

eladnava commented Jul 20, 2016

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).

@hypesystem
Copy link
Collaborator

Alright :-) let's do it

@dan-perron
Copy link
Author

For your consideration #264

@eladnava
Copy link
Collaborator

eladnava commented Oct 3, 2016

Closing this as PR was merged successfully. 😄

@eladnava eladnava closed this as completed Oct 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants