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
2. http://spion.github.io/posts/why-i-am-switching-to-promises....
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.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.
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.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.
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.