Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Created May 6, 2015 14:38
Show Gist options
  • Save tobiashm/e17b3efce938a1dfeb82 to your computer and use it in GitHub Desktop.
Save tobiashm/e17b3efce938a1dfeb82 to your computer and use it in GitHub Desktop.
Convert Node callback to Promise
function callbackAsPromise(fn, scope) {
return function() {
var args = Array.prototype.slice.apply(arguments);
return new Promise(function(resolve, reject) {
fn.apply(scope, args.concat(function(err, data) {
if (err) reject(err);
resolve(data);
}));
});
}
}
// Example:
// var readFile = callbackAsPromise(fs.readFile, fs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment