0

I understand the notion of passing a function as a variable in javascript - whether it be a function variable or an anonymous function. However, I don't understand when or why a function should be executed as a parameter instead of before or after the primary function call.

For example:

Assuming a callback function and a private function are created -

function callback(y) {
       alert(y);
    }

function foo(x, callback) {
       alert(x);
    }

what's the difference between these two statements and when might the latter one be more practical than the first?

foo(bar);
callback(bas);

or

foo(bar, callback(bas));

I'm sure this isn't syntactically correct, but hopefully it illustrates what I'm trying to convey.

Ultimately, I'm trying to understand when running a function as a parameter is practical.

  • Callbacks are usually called asynchronously. Calling them synchronously, after a call to a function which initiates an asynchronous operation, defeats the purpose of waiting until the asynchronous operation succeeds or fails. – traktor Sep 09 '17 at 05:47
  • "Callback" is a bit of a misnomer here so I probably shouldn't have used it. I just happened to realize this scenario while using a callback. As I stated, I'm ultimately trying to understand when and why a function should be executed as a parameter as opposed to an independent function call - regardless of whether it's a callback or not. – Squire4Hire Sep 09 '17 at 06:17
  • Possible duplicate of [What is a callback function?](https://stackoverflow.com/questions/824234/what-is-a-callback-function) – Patrick Roberts Sep 09 '17 at 06:25

2 Answers2

1

When the language allows to pass functions just like any other objects, they are called first class functions. They give the ability to code in a new paradigm of programming called Functional Programming.

If the code is synchronous , then there would be no difference in the order of execution of foo(bar) and callback(bas)

But if the foo function is async and callback is not, then if you execute

foo(bar)
callback(bas)

callback would be executed first and foo later. Where as if you pass as an argument, callback execution can be delayed till after foo is executed.

Also callback would be able to access the variables of foo() function if its passed as argument because of closure even after foo() has completed the execution.

Vijay
  • 286
  • 1
  • 9
  • "Also callback would be able to access the variables of foo() function if its passed as argument because of closure even after foo() has completed the execution." -- This, to me, appears to be an exceptionally valid reason to execute a function as a parameter which is really what I was trying to get at. Thanks for seeing through the 'callback' and recognizing the confusion of parameter executed functions. – Squire4Hire Sep 09 '17 at 06:21
  • Great that it helped :) – Vijay Sep 09 '17 at 07:02
0

As posted, the foo function does not use its callback parameter, so there is no effective difference between callback and foo.

Missing is that callbacks are usually called asynchronously. Calling them synchronously, in caller code, after a call to a function which initiates an asynchronous operation, defeats the purpose of waiting until the asynchronous operation succeeds or fails before initiating the callback action.

Indecision can occur if the operation performed by an operation function provided with a call back (function value) somehow manages to perform the operation synchronously. Should the callback be made synchronously, before the operation function returns, or asynchronously in an event loop call out after the operation function returns to its caller? The ECMAScript Promise model is the later - make the callback asynchronously, some time later.

traktor
  • 17,588
  • 4
  • 32
  • 53
  • Does this really explain when and why a function would/should be executed as a parameter instead of an independent function? This seems to just add more confusion. – Squire4Hire Sep 09 '17 at 06:23