4 ms·
What version of Javascript has Promises and does anyone know the estimated time it might take to make it over to V8 and subsequently node.js? The biggest issue
by iamwil 13y ago
What version of Javascript has Promises and does anyone know the estimated time it might take to make it over to V8 and subsequently node.js?
The biggest issue I had with promises is interfacing with non-promise code. There are methods to 'lift' node.js code that uses callbacks into promises, but the resulting code is much noiser.
I'm a bit disappointed that the official JS version didn't cut down on the visual noise of the piping so the actual work being done stands out. Maybe there needs to be a different type of syntax highlighter that mutes the promises piping and highlights the actual calls.
- astrodust 13y agoI've just been using the Q promise library and it works fine in NodeJS. The Q wrappers for NodeJS style functions is a bit ugly, but it works very well. The real problem is the core NodeJS libraries aren't promise compliant. I don't know if there's a module that cleanly wraps all of these in their promise analogs but that'd be the best thing here. Calling Q.nsend() on everything is messy, but is a lot better than dealing with ridiculously nested callbacks.
- mjackson 13y ago> The real problem is the core NodeJS libraries aren't promise compliant. This isn't a problem at all in practice. To say that node core is not "promise compliant" gives people the idea that there is something that prevents them from using promises in node. In reality, most promise libraries have a "denodeify" function that will convert any function that expects a node-style callback into one that returns a promise instead. var fs = require('fs'); var rsvp = require('rsvp'); // Create a version of fs.readFile that returns // a promise instead of expecting a callback. var readFile = rsvp.denodeify(fs.readFile); readFile('/etc/passwd', 'utf8').then(function (contents) { // do something with the file contents }, function (error) { // handle the error }); It is similarly easy to convert functions that expect other types of callbacks (read: not node-style callbacks where the first argument is always an error) to functions that return promises using deferreds.
- astrodust 13y agoThat's a good pattern, but obnoxious to apply on large numbers of functions. If something just did this for you in advance, that'd be what I'm looking for. Trouble is, NodeJS set a convention and now everyone adheres to it. I'm not complaining there's standards, in fact that's a good thing. Part of me is just disappointed that the promise standardization happened too late in the game for NodeJS to build around it.
- spion 13y agoIt really isn't much of a problem. A lot of the time its as easy as replacing var lib = require('lib'); with (given var Promise = require('bluebird')) var lib = require('lib'); Promise.promisifyAll(lib); And in case of classes, you'd use promisifyAll on about 2-3 prototypes instead.
- kybernetikos 13y agoI assume that some future major revision of node will move its API across and that in the meantime, promise versions of core libraries will start appearing on npm.
- domenicd 13y ago> The real problem is the core NodeJS libraries aren't promise compliant. I don't know if there's a module that cleanly wraps all of these in their promise analogs but that'd be the best thing here. There's a few of these in npm, but I think the best one is probably the pr package [1], which lets you do e.g. var fs = require("pr/fs") and then use Bluebird-promise returning versions of the fs methods. [1]: https://npmjs.org/package/pr https://npmjs.org/package/pr
- JoshGlazebrook 13y agoHave you looked into bluebird[1]? It and most of the available promise libraries have a nodify-esque function where you can simple do this: var fs = Promise.promisifyAll(require("fs")); And all of the regular methods in fs are now promisable. Q also seems to be one of the slowest[2] promise libraries there is. I'm not sure why so many people use it. That guy has a rather large benchmark table from a couple months ago as well and Q seems to be shockingly slow compared to the dozen or so other promise libraries out there. 1. https://github.com/petkaantonov/bluebird https://github.com/petkaantonov/bluebird 2. http://spion.github.io/posts/why-i-am-switching-to-promises.html http://spion.github.io/posts/why-i-am-switching-to-promises....
- domenicd 13y agoPromises are in ES6. There is a prototype version of them in V8 behind the usual --harmony flag, although it is very bad and deviates from the standard quite a lot. E.g. it passes 221/879 of the Promises/A+ tests [1], which only test the then method; it also deviates on other methods, as noted in the article [2]. Node tends to keep up with V8 versions pretty well, so I anticipate by the time Node 0.12 is released the --harmony flag will enable the Promise global. (Hopefully by then the V8 team will have gotten their act together on standards compliance.) Node core though is pretty committed to the error-first callback pattern; that will be a longer shift, I think. [1]: https://twitter.com/promisesaplus/status/407721467778846720 https://twitter.com/promisesaplus/status/407721467778846720 [2]: http://www.html5rocks.com/en/tutorials/es6/promises/#toc-api http://www.html5rocks.com/en/tutorials/es6/promises/#toc-api
- paulirish 13y agoThe implementation in Blink is now passing the Promises/A+ tests at 100% [1], and we'll be updating the V8 implementation accordingly. We want to be shipping the one in V8 ASAP, but as you note it needs some work, so we'll be addressing those issues and then getting it out the door very soon. [1]: https://twitter.com/ChromiumDev/status/413035115665575936 https://twitter.com/ChromiumDev/status/413035115665575936