lib/altshift/promise.js
function (asyncFunction, callbackNotDeclared) {
    var arity = asyncFunction.length;

    return function () {
        var deferred = new Deferred(),
            args = slice.call(arguments),
            callback;
        if (callbackNotDeclared && !args[args.length + 1] instanceof Function) {
            arity = args.length + 1;
        }
        callback = args[arity - 1];

        args.splice(arity);
        args[arity - 1] = function (error, result) {
            if (error) {
                deferred.emitError(error);
            } else {
                if (args.length > 2) {
                    // if there are multiple success values, we return an array
                    args.shift(1);
                    deferred.emitSuccess(args);
                } else {
                    deferred.emitSuccess(result);
                }
            }
        };
        asyncFunction.apply(this, args);

        //Node old school
        if (callback) {
            deferred.then(
                function (result) {
                    callback(Promise.NO_ERROR, result);
                },
                function (error) {
                    callback(error, Promise.NO_RESULT);
                }
            );
        }

        return deferred.getPromise();
    };
}
function (asyncFunction, callbackNotDeclared) {
    var arity = asyncFunction.length;

    return function () {
        var deferred = new Deferred(),
            args = slice.call(arguments),
            callback;
        if (callbackNotDeclared && !args[args.length + 1] instanceof Function) {
            arity = args.length + 1;
        }
        callback = args[arity - 1];

        args.splice(arity);
        args[arity - 1] = function (error, result) {
            if (error) {
                deferred.emitError(error);
            } else {
                if (args.length > 2) {
                    // if there are multiple success values, we return an array
                    args.shift(1);
                    deferred.emitSuccess(args);
                } else {
                    deferred.emitSuccess(result);
                }
            }
        };
        asyncFunction.apply(this, args);

        //Node old school
        if (callback) {
            deferred.then(
                function (result) {
                    callback(Promise.NO_ERROR, result);
                },
                function (error) {
                    callback(error, Promise.NO_RESULT);
                }
            );
        }

        return deferred.getPromise();
    };
}
function ( asyncFunction , callbackNotDeclared ) { var arity = asyncFunction . length ; return function ( ) { var deferred = new Deferred ( ) , args = slice . call ( arguments ) , callback ; if ( callbackNotDeclared && ! args [ args . length + 1 ] instanceof Function ) { arity = args . length + 1 ; } callback = args [ arity - 1 ] ; args . splice ( arity ) ; args [ arity - 1 ] = function ( error , result ) { if ( error ) { deferred . emitError ( error ) ; } else { if ( args . length > 2 ) { // if there are multiple success values, we return an array args . shift ( 1 ) ; deferred . emitSuccess ( args ) ; } else { deferred . emitSuccess ( result ) ; } } } ; asyncFunction . apply ( this , args ) ; //Node old school if ( callback ) { deferred . then ( function ( result ) { callback ( Promise . NO_ERROR , result ) ; } , function ( error ) { callback ( error , Promise . NO_RESULT ) ; } ) ; } return deferred . getPromise ( ) ; } ; }
Converts a Node async function to a promise returning function

@param {Function} asyncFunction node async function which takes a callback as its last argument
@param {boolean} callbackNotDeclared true if callback is optional in the definition
@return {Function} A function that returns a promise
Converts a Node async function to a promise returning function