Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Add promisify method to util #7642

Closed
benjamingr opened this issue May 18, 2014 · 15 comments
Closed

Add promisify method to util #7642

benjamingr opened this issue May 18, 2014 · 15 comments

Comments

@benjamingr
Copy link
Member

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 as fn but returns a promise.

So, something like:

var fs = require("fs");
var readFileAsync = util.promisify(fs.readFile);
readFileAsync().then(function(contents){
      // contents of file available here
}).catch(function(err){
     // handle error from .then, or I/O error here
});

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:

var apiCall = util.nodeify(promiseCall);
apiCall(function(err,result){
     // err and result here are defined in the node convention
});

What are your opinions about this? Would a PR be entertained?

@Zirak
Copy link

Zirak commented May 18, 2014

This is a good idea. +1

@oimou
Copy link

oimou commented May 18, 2014

Like! +1

@indutny
Copy link
Member

indutny commented May 18, 2014

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?

@vkurchatkin
Copy link

@benjamingr
Copy link
Member Author

@indutny Yes, fast promisifcation would require internals

@indutny
Copy link
Member

indutny commented May 18, 2014

@benjamingr could you please paste some code or links to existing stuff that would prove your assumption?

@TonyCronin
Copy link

good idea +1

@TooTallNate
Copy link

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.

@petkaantonov
Copy link

@indutny

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.

@vkurchatkin
Copy link

@petkaantonov just to be clear, are you talking about v8 code modification?

@petkaantonov
Copy link

Just saying that you need access to promiseRaw, PromiseResolve and PromiseReject to do anything fast.

@TooTallNate
Copy link

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

@trevnorris
Copy link

If, in the very unlikely event that suck functionality was added, the only
support would be for the V8 built in. Which, currently is broken.

Regardless, the future of Node is to minimize abstraction and footprint so
such things can more easily be implemented in user land.

I don't see this happening in core.

@benjamingr
Copy link
Member Author

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.

@tjfontaine
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants