0

I was reading https://docs.angularjs.org/api/ng/function/angular.noop which has this example:

function foo(callback) {
  var result = calculateResult();
  (callback || angular.noop)(result);
}

I can't work out what this line is doing:

  (callback || angular.noop)(result);

It looks like callback and angular.noop are functions that can return true or false, but what is the value used for, and what does (result) do next to it?

1 Answer 1

1

The left side determines which function reference to use and the right side invokes that function passing in result as parameter

In more verbose form it is the same as doing:

if (callback) {
  callback(result)
} else {
  angular.noop(result)
}
Sign up to request clarification or add additional context in comments.

3 Comments

So it calls callback, which returns true or false, and then if it's true, it once again calls callback passing in result as the argument?
More like using the outer function with or without passing in a callback. foo() vs foo(function(result){ /* do something*/})
I understand now. The callback in the if statement is not being called (because it doesn't have () after it), it's just being tested for null/undefined. If it's not null/undefined then it will be called with result as the argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.