Skip to content

Instantly share code, notes, and snippets.

@geraldyeo
Created September 29, 2013 23:26
Show Gist options
  • Save geraldyeo/6757479 to your computer and use it in GitHub Desktop.
Save geraldyeo/6757479 to your computer and use it in GitHub Desktop.
<http://howtonode.org/promises> The majority of functions that take Node-style callbacks are suitable for wrapping in a Promise. Any Node-style callback function that only calls its callback one time may be wrapped. The following function is taken from Bogart.
function promisify(nodeAsyncFn, context) {
return function() {
var defer = q.defer()
, args = Array.prototype.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;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment