Skip to content

Instantly share code, notes, and snippets.

@mrbobbybryant
Created December 14, 2015 01:35
Show Gist options
  • Select an option

  • Save mrbobbybryant/1aaae6e087b8c3996337 to your computer and use it in GitHub Desktop.

Select an option

Save mrbobbybryant/1aaae6e087b8c3996337 to your computer and use it in GitHub Desktop.
This function allows you to only call a function if it has everything it needs to run. So it "May" run.
function maybe(fn) {
return function() {
var i;
if ( arguments.length === 0 ) {
return;
} else {
for(i = 0; i < arguments.length; i++) {
if (arguments[i] == null) return;
}
return fn.apply(this, arguments);
}
};
}
var empty;
function addOne(num) {
return num + 1;
}
console.log( maybe(addOne)(empty) );
console.log( maybe(addOne)() );
console.log(maybe( addOne )(1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment