Skip to content

Instantly share code, notes, and snippets.

@taktran
Created April 26, 2015 10:41
Show Gist options
  • Save taktran/2617b45365c849aa89fb to your computer and use it in GitHub Desktop.
Save taktran/2617b45365c849aa89fb to your computer and use it in GitHub Desktop.
applyParamsToArgCallback [NOT WORKING]
"use strict";
var _ = require('lodash');
var utils = {};
/**
* Apply the rest of the arguments to the last
* item in function arguments (the callback)
*
* ```
* arguments = [fnArguments, firstParamForCallback, secondParamForCallback, ...]`
*
* functionArguments = [firstParam, secondParam, ..., callback]
* ```
*
* @example
*
* function something() {
* utils.applyParamsToCallback(arguments, 1, 2, 3)
* // Calls arguments[last](1, 2, 3)
* }
*
*/
utils.applyParamsToArgCallback = function() {
var thisArgs = Array.prototype.slice.call(arguments);
fnArgs = fnArgs || [];
fnArgs = Array.prototype.slice.call(fnArgs);
var callback = fnArgs.pop();
var fnParams = _.rest(thisArgs) || [];
callback.apply(undefined, fnParams);
};
module.exports = utils;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment