-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Consistent sync and async function names #5
Comments
Haha, let the discussion begin! :) |
I believe it's just crypto that still has problems right? Some places where async/non-blocking could help
|
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. |
@iefserge related issue nodejs/node-v0.x-archive#7323. Proof of concept 1: https://github.com/vkurchatkin/deasync |
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. |
@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) |
@Naman34 that is already happening above core, see: In general, if something is being accomplished well in the ecosystem then core should stay out of it. |
@iefserge I'd argue that we probably need less synchronous functions, not more. Having a Actually I'd be interested in what would happen if all |
@yoshuawuyts the module system relies on a bunch of sync calls, so they can't be removed entirely. |
Agreed. This seems uncontroversial.
I'm not sure what you mean here. That you need to buffer before you can encrypt/decrypt/verify? |
It's possible, but |
or simply a promise. |
Promises, like the many other async abstractions that exist, belong in user modules. |
@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. |
Yes, and that is why they are a part of the language |
@vkurchatkin Current promises implementation in v8 is very slow, too much overhead to create a promise for every async operation. Simple int id would work. |
@vkurchatkin Promise is going to be part of the language soon. But it's not the right approach for all things yet. Promises suck for event handlers, like the callback for http.createServer. Yes, there are other places where Promises are awesome to work with. But It's nice to have consistency in the API. And wrapping core features in Promises is SO TRIVIAL, it's not even worth messing with the core to save you a few lines of code per project. Also, I really like what Pete Hunt said, |
@iefserge I honestly don't think that promises can be a bottleneck. Eventually they could be fast and well optimized, so this is a sort of investment. Your idea requires weak maps (I think), otherwise memory leaks are possible: var registry = new WeakMap;
function asyncFn () {
var id = Symbol();
var operation = { done : false, result : null, error : null };
registry.set(id, operation);
someCallbackBasedFn(function (err, res) {
operation.done = true;
if (err)
operation.error = err;
else
operation.result = res;
});
return id;
} If we store registry as a |
@Naman34 event listeners and cps callbacks are not the same. Listeners should be simple functions, no doubt. var server = createServer(function (request) {
return new Promise(function (resolve, reject) {
resolve(new http.Response({/* some options */}))
})
}); |
@bnoordhuis when you decrypt something with GCM, you shouldn't do anything with the decrypted data until you authenticate it, which means in practice you should buffer it on decryption. |
Node doesn't intentionally make things slower. Ever. @trevnorris would literally burn this whole mother down, and with good reason. There aren't actually any applications in Node that don't use third party modules outside of userland. The fact that promises can be accomplished easily in userland with no performance hit to the rest of core means they'll likely never be added to core itself. That doesn't mean people can't use them and build an ecosystem with them, you're more than welcome to, but it's silly to continue to ask for core to adopt them when there is a measurable penalty for doing so. |
whoa whoa this issue for me was just about deprecating primarily |
@jonathanong maybe you can update the title and description to be more specific and limit the scope :) |
can we create a |
@jonathanong we're talking about core, it should probably be an issue in here. |
That seems perfectly acceptable to me. I pulled the name out of thin air when I added crypto.randomBytes(), no thought was put into it. In retrospect, it's kind of an odd duck compared to other sync/async core APIs. |
@mikeal Thanks. I need to write a post about core API and Promises/etc. that we can point to. This is a topic that's continuously brought up. |
About adding API's like |
@trevnorris do you object to just making the |
…r-resume Downgrade Node version due to https://github.com/nodejs/node/issues/5…
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 likecrypto.randomBytes(length)
in favor ofcrypto.randomBytesSync(length)
.There is no request for additional functionality. PR welcomed?
Reference: nodejs/node-v0.x-archive#7030
The text was updated successfully, but these errors were encountered: