/**
* Wraps a Node.JS style asynchronous function `function(err, result) {}`
* to return a `Promise`.
*
* @param {Function} nodeAsyncFn A node style async function expecting a callback as its last parameter.
* @param {Object} context Optional, if provided nodeAsyncFn is run with `this` being `context`.
*
* @returns {Function} A function that returns a promise.
*/
q.promisify = function(nodeAsyncFn, context) {
return function() {
var defer = q.defer()
, args = slice.call(arguments);
args.push(function(err, val) {
if (err !== null) {
return defer.reject(err);
}
return defer.resolve(val);
});
nodeAsyncFn.apply(context || {}, args);
return defer.promise;
};
};
https://github.com/nrstott/bogart/blob/v0.7.2/lib/q.js#L13