Skip to content

Instantly share code, notes, and snippets.

@acatl
Last active August 29, 2015 14:07
Show Gist options
  • Save acatl/bdb6e4e867f08bd18bd8 to your computer and use it in GitHub Desktop.
Save acatl/bdb6e4e867f08bd18bd8 to your computer and use it in GitHub Desktop.
callback slice args
function callbackSliceArgs(callback, howMany) {
return function() {
var args = _.toArray(arguments);
args.splice(howMany, args.length);
return callback.apply(null, args);
};
}
@acatl
Copy link
Author

acatl commented Oct 18, 2014

use it when you only need to get passed the first X arguments to the callback, useful when using array/collection transformation methods

function foo(a, b) {
   console.log(a,b);
}

var pfoo = _.partialRight(foo, 'b');
_.forEach([1, 2, 3, 4], callbackSliceArgs(pfoo, 1) );

will print out:

1, 'b'
2, 'b'
3, 'b'
4, 'b'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment