You have some errors in your examples.
READ1
This is not a βmake a promise by handβ, it is just a normal asynchronous call. In your code, you call readFile immediately, so read1 will be the return value of readFile , which is undefined . To get behavior like read2 and read3 , you need to do something like this:
var read1 = function(fname, env, success, error){ fs.readFile(fname, enc, function (err, data) {
read2
read3
var read3 = function (fname, enc) { var deferred = Q.defer(); fs.readFile(fname, enc, function (error, text) { if (error) {
You can really use nfbind for anything that takes a callback as the last parameter. With my comments, read2 and read3 accomplish the same purpose, which is to create a function that will take a file name and encoding and return a promise object.
For this you can do this:
read2('file.txt', 'utf8').then(function (data) {}, function (err) {}).done(); read3('file.txt', 'utf8').then(function (data) {}, function (err) {}).done();
For read1 , you would call it like this:
read1('file.txt', 'utf8', function (data) {}, function (err) {});
Update
The promises standard has changed a bit since it was answered, and if you are leaning towards read3 , I would recommend doing the following:
var read4 = function (fname, enc) { return Q.promise(function(resolve, reject){ fs.readFile(fname, enc, function (error, text) { if (error) {
This is more in line with the ES6 promises standard and the blue bird, so it will be easier for you to move forward. Using the method mentioned in read3 , the ability to synchronously throw exceptions instead of catching them in the promise chain is also introduced, which is usually undesirable. See delayed antipattern .