You can use Q.nfcall
to call a NodeJS function promisified.
function httpGet(opts){
return Q.nfcall(http.get,opts);
}
Or simply:
var httpGet = Q.nfbind(http.get,http)
This would also work for fs.readFile
by the way.
If you want to do it manually. Q's deferred objects have a .makeNodeResolver
function which lets you pass them around safely like you do:
var httpGet = function (opts) {
var deferred = Q.defer();
http.get(opts, deferred.makeNodeResolver());
return deferred.promise;
};
One of the things .makeNodeResolver
does is bind the .this
value.
It is better to use .nfcall
though.