last-callback v2.0.1
Extract function's last argument and wrap with ready to invoke function. If last argument is callable, hence callback - wrapper will invoke it. Otherwise invoked wrapper do nothing.
Description
Last callback provides callable wrapper for last argument. There is no need for further checking if last argument is callable.
Wrapping works for infinite number of arguments even undeclared one.
Requirements
NodeJS >= 8.9.0
Version for browsers is compatible with ES5.
installation
with NPM:
npm install -S last-callback
For older NodeJS
npm install -S last-callback@1.0.3
Optional callback as last argument
Scenario: There is "execute" function which expect last argument to be optional callback.
const lastCallback = ; { // 'arguments' is magic variable always visible inside function. let callback = ; let sum = x + y; ; return sum;} ;
Undeclared optional callback as last argument
Scenario: There is "make" function which allow callback as last argument.
TIP Undeclared arguments will NOT be visible for debugger and IDE inspection.
const lastCallback = ; { // 'arguments' is magic variable always visible inside function. let callback = ; let result = x + y * z; ; return result;} // make declared 3 arguments: 'x', 'y' and 'z'// but passing fourth argument is completely legal// and JS will not complain.;
Calling callback with different context
Scenario: There is 'execute' function which expect callback. There is also 'spy' object, which alter callback context.
const lastCallback = ; { if thissecret === undefined thissecret = secret; return thissecret;} // returns 'secret' without change { return secret;} // returns 'secret' from different context { return ;} secretPassword = 'I am real secret'; // will return 'I am real secret'; // will return 'Luke I am your father';
Recursive callback
Scenario: There is a 'execute' function which expected callback. The callback will be recursive.
const lastCallback = ; { if n < t console ; } { start stop;} ;// console log result:// 0// 1// 2// 3// 4
Contribution
Did you find any bugs?
Maybe this documentation has language mistakes?
You have idea for great new feature?
Create new issue and describe your point of view. I will do my best to meet all requests.
This repository is open for changes and suggestions. I encourage you to write your own solution and make pull request.
LICENSE
The MIT License (MIT) Copyright (c) 2016 Paweł Zadrożny