-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Add promisify method to util #7642
Comments
This is a good idea. +1 |
Like! +1 |
So the main question, as usual, is why should it be in core and not in 3rd party module? Does it depend on anything internal? |
@indutny Yes, fast promisifcation would require internals |
@benjamingr could you please paste some code or links to existing stuff that would prove your assumption? |
good idea +1 |
I see no reason for this to be in core. There's already plenty of 3rd party modules that handle this just fine. callbackify being my personal, minimal, favorite. |
With internals you can do https://github.com/v8/v8/blob/master/src/promise.js#L87-L88 Without internals, you need to allocate your own closure, and the implementation then needs to allocate 2 more. |
@petkaantonov just to be clear, are you talking about v8 code modification? |
Just saying that you need access to |
@petkaantonov the Node.js core JavaScript code has the same Promise access that 3rd party modules get. We don't get access to the V8 internals. |
If, in the very unlikely event that suck functionality was added, the only Regardless, the future of Node is to minimize abstraction and footprint so I don't see this happening in core. |
Fair enough, If you're all sure you can't bring anything to the table that user land code can't I'm fine with not doing this. Especially since NodeJS promises are still a bit immature, and are not as fast as callbacks like Bluebird promises. That said, given Promises are a native language feature and are here to stay for node versions to come, I'd like to see it easier for developers to play with the NodeJS core libraries with them without third party code, but that's definitely something that can be delayed. Feel free to close if you feel that the discussion has exhausted itself. |
As I've outlined in other issues, the short and medium term for Node is that promises based APIs are not in the cards. The VM implementors are doing what they want and complying with what ES6 is doing, but that doesn't change the position of Node. If there are APIs that can be improved to more easily support external modules to promisifying core please file issues/pull requests for those specific things and we will do what we can to accommodate those needs. |
Given promises are present in 0.12.* (which is cool), and that the API remains the same in 0.12 and remains in its nodeback form (which is cool, and I completely agree with). I'd like to propose a utility method to interop between promises and nodebacks.
No changes need to be made to any APIs, all NodeJS APIs remain with nodebacks (which, I'm all for). In my opinion, it should be very easy for a developer to choose which method of asynchronousity they prefer and move back and forth between promises and nodebacks.
util.promisify(fn) - takes a function whose last argument is in the form of
function(err,result){
and returns a new function that does exactly the same asfn
but returns a promise.So, something like:
You can see for example @petkaantonov 's
Promise.promisify
function and what it does. Note that it works with almost 0 overhead.I'd also like to propose a
util.nodeify
function (needs a better name) that takes a promise, and returns a node callback version of it. This way, people who have to use promisified modules can get callbacks instantly:What are your opinions about this? Would a PR be entertained?
The text was updated successfully, but these errors were encountered: