Created
May 6, 2015 14:38
-
-
Save tobiashm/e17b3efce938a1dfeb82 to your computer and use it in GitHub Desktop.
Convert Node callback to Promise
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 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