Created
September 29, 2013 23:26
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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