Created
May 27, 2015 19:28
-
-
Save m1sta/6510668759ee42a647f0 to your computer and use it in GitHub Desktop.
Utility function to convert a function with signature fn(args, cb(err, result)) to return a 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
//Utility function to convert a function with signature fn(args, cb(err, result)) to return a promise | |
function promisify ( fn, context ) { | |
return function () { | |
var args = [].slice.call( arguments ); | |
return new Promise( function ( fulfil, reject ) { | |
var callback = function ( err ) { | |
if ( err ) return reject( err ); | |
fulfil.apply( null, [].slice.call( arguments, 1 ) ); | |
}; | |
args.push( callback ); | |
fn.apply( context, args ); | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example...