Skip to content
Get a callback function from arguments
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.editorconfig
.eslintrc
.gitattributes
.gitignore
.jscs.json
.travis.yml
LICENSE
README.md
appveyor.yml
bower.json
component.json
get-callback-cjs.js
get-callback.js
package.json
test.js

README.md

get-callback.js

Build Status Build status Coverage Status devDependency Status

Get a callback function from arguments

function printCallback() {
  var cb = getCallback(arguments);
  console.log(cb.toString());
}

printCallback('foo', {some: 'options'}, function cb(x, y) { return x + y; });

//yields: 'function cb(x, y) { return x + y; }'

This module is useful to create a function which takes a callback as its last argument.

Installation

Package managers

npm NPM version

npm install --save get-callback

bower Bower version

bower install --save get-callback

Standalone

Download the script file directly.

API

getCallback(args)

args: Array or array-like object (e.g. arguments)
Return: Function

It returns the last element of args, when the element is a function.

It throws an error when the last element is not a function.

function cb() {
  //... Something to do in the callback
}

(function() {
  getCallback(arguments); //=> [Function: cb]
})('foo', {some: 'options'}, cb);

License

Copyright (c) 2014 Shinnosuke Watanabe

Licensed under the MIT License.

Something went wrong with that request. Please try again.