0

I am trying to understand Callback Functions and am a bit confused. Now, if I pass 3 or more functions in the parameters and then call them one after another, as in below example, then will they all be called Callback Functions? or the parent will be known as Callback Function? or Both will together be called as Callback Functions?

Also, I have never heard that a function having more than 1 callbacks. So, is this example a correct reference of such example ... and so, will it be correct to say - A function can have infinite no of callbacks? Kindly, verify and explain!

function alpha(a, b, 
  fn1 = ()=>{
    console.log("hello");
  },
  fn2 = () => {
    console.log("good morning")
  },
  fn3 = () => {
    console.log("good evening")
  }){
  console.log(a, b, a+b);
  fn1();
  fn2();
  fn3();
}
alpha(5, 10);

Reference:

Callback Function is not working in JavaScript example

Deadpool
  • 7,811
  • 9
  • 44
  • 88
  • 2
    That's all semantics, with as many opinions as there are people answering. Is there some problem behind this question? – Thomas Jul 22 '20 at 15:16
  • Passing functions is just a way to generalize other functions. IME, these are called callback when there are some "events" produced by the main function that may need specially handling from the calling function (e.g., success and failure parameters which allow the caller to define the behavior in those scenarios) – Ryan Emerle Jul 22 '20 at 15:22

1 Answers1

1

If I pass 3 or more functions in the parameters and then call them one after another, will they all be called Callback Functions?

Yes. It doesn't even have to call them one after another or only once, the intention to possibly call them is enough.

Or the parent will be known as Callback Function? or both together?

No.

alpha is a callback-accepting function.

Also, I have never heard that a function having more than 1 callbacks

The promise then method is a good example for this: it calls either of the two callbacks passed to it, depending on a condition. There are more examples, most of them probably distinguishing between success and error cases, but in general there is no limit in the number of parameters.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375