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

Introducing Promises to node core? #4596

Closed
bevacqua opened this issue Jan 8, 2016 · 10 comments
Closed

Introducing Promises to node core? #4596

bevacqua opened this issue Jan 8, 2016 · 10 comments
Labels
discuss Issues opened for discussions and feedbacks. feature request Issues that request new features to be added to Node.js.

Comments

@bevacqua
Copy link

bevacqua commented Jan 8, 2016

Even though Promises were removed back in the early days, the situation has changed. I propose we re-introduce Promises to Node.js, (mostly) without introducing breaking changes.

  • People seem to be in favor of introducing promises to Node.js
  • Node.js offers better support for promises than it used to
  • Promises are very popular
  • Promises are now a part of the language
  • Promises play well with generators (and eventually async/await)
  • User-land is filled with things that turn callback-based API into promise-based
  • Newly introduced APIs in browsers consistently offer promise-based continuation -- fetch, ServiceWorker, etc.

Encouraging Use of Promises

ES6 in and out of itself is encouraging more and more developers to use promises, and if Node.js were to add support for promises that'd send a clear signal to the community that promises have widespread support.

In a way, keeping promises out of core is more opinionated than not.

I understand most noders prefer callbacks, as I used to (now I don't mind either way). However, they're not that terrible to work with if you're familiarized with them. In addition, promises now being part of the language means more libraries leveraging and written around promises.

As a Breaking Change

This change would break shorthand notation where people write code like this, although in these situations people hardly ever use the return value of foo.

function foo (bar, baz, done) {
  if (bar) {
    // instead of "safer" fs.readFile(bar, done); return;
    return fs.readFile(bar, done);
  }
  fs.readFile(baz, done);
}

That being said it's a very real possibility that introducing a Promise result to callback-based APIs where no responses were assumed could signify a breaking change in cases where poor coding practices are followed.

function foo (bar, done) {
  if (!bar) {
    return new Error('terrible idea');
  }
  fs.readFile(bar, done);
}

Scope

While all of the modules below offer callback-based APIs, it wouldn't be that hard to provide promise based APIs for most of these. Particularly because most promise-based methods could be a wrapper around the callback-based API.

  • readline methods offering a callback
  • repl methods offering a callback
  • fs methods offering a callback
  • util methods offering a callback
  • crypto methods offering a callback
  • zlip methods offering a callback
  • child_process methods offering a callback
  • cluster methods offering a callback
  • process methods offering a callback
  • http methods offering a callback
  • https methods offering a callback
  • net methods offering a callback
  • tls methods offering a callback
  • dgram methods offering a callback
  • dns methods offering a callback
  • stream methods offering a callback

Obviously, the implementation wouldn't have to offer every single callback-based method in a promise-based flavor. For instance, it could start with just fs methods.

@mscdex
Copy link
Contributor

mscdex commented Jan 9, 2016

One of my main concerns with adding support for (standard) Promises is performance. My other major concern is backwards compatibility issues (including the examples you touched on) and the option to still use callbacks.

With regard to the "browsers are doing it" argument, IMHO that doesn't really matter because node isn't a browser and as such doesn't have a DOM or any of the other browser-specific things you mentioned. Also, votes for a random twitter poll that is less than a day old is hardly representative of the node.js community/ecosystem.

FWIW previous discussion on this matter can be found here.

@mscdex mscdex added future discuss Issues opened for discussions and feedbacks. feature request Issues that request new features to be added to Node.js. labels Jan 9, 2016
@mscdex
Copy link
Contributor

mscdex commented Jan 9, 2016

Discussion around this is probably better suited to the existing Promise issue over in the nodejs/NG repo (which is for discussing long term changes in node) here.

@cjihrig
Copy link
Contributor

cjihrig commented Jan 9, 2016

Also see #11. I'm going to close this as a duplicate of #11 and in favor of nodejs/NG#25.

@cjihrig cjihrig closed this as completed Jan 9, 2016
@bevacqua
Copy link
Author

bevacqua commented Jan 9, 2016

Thanks for the reference to nodejs/NG#25. I'll move the discussion to there.

@ORESoftware
Copy link
Contributor

It looks like in Node.js version 9, Promises were added to core? At least for fs?
where can I get info on that?

@ORESoftware
Copy link
Contributor

anyone?

@BridgeAR
Copy link
Member

BridgeAR commented Dec 9, 2017

@ORESoftware I am not sure where you got that info from. The pull request to add that is still open and WIP. If you need to find information about features, please check the changelog and the public API documentation. You will find everything published in those places.

@ORESoftware
Copy link
Contributor

Yeah I can't remember, I caught wind of it about 3 or 4 weeks ago

@tunnckoCore
Copy link

@BridgeAR can you give link to that PR? For when are the plans to be released, v10?

@richardlau
Copy link
Member

#18297 landed three days ago on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Issues opened for discussions and feedbacks. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

7 participants