Include this snippet to promisify any node function that uses the standard form:
var Promise = require( 'es6-promise' ).Promise,
promisify;
promisify = (function ( slice ) {
return function ( 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 );
});
};
};
}( [].slice ));
Then use it like this:
var standard = function ( arg1, arg2, arg3, callback ) {
// callback is eventually called with two arguments - an error
// (if applicable) and the result;
};
var promisified = promisify( standard );
promisified( arg1, arg2, arg3 ).then( handleResult, handleErr );
You can optionally pass in a context:
var fs = require( 'fs' ),
readFile = promisify( fs.readFile, fs );