What is Angular.noop used for?
Solution 1:
angular.noop is an empty function that can be used as a placeholder when you need to pass some function as a param.
function foo (callback) {
// Do a lot of complex things
callback();
}
// Those two have the same effect, but the later is more elegant
foo(function() {});
foo(angular.noop);
Solution 2:
I find it extremely helpful when writing a function that expects a callback.
Example:
function myFunction(id, value, callback) {
// some logic
return callback(someData);
}
The function above will return an error, when it gets called without specifying the third argument. myFunction(1, 'a');
Example (using angular.noop
):
function myFunction(id, value, callback) {
var cb = callback || angular.noop; // if no `callback` provided, don't break :)
// some logic
return cb(someData);
}
Solution 3:
It is a function that performs no operations. This is useful in situation like this:
function foo(y) {
var x= fn();
(y|| angular.noop)(x);
}
It is useful when writing code in the functional style