0

I don't understand the use of the general word callback. I am investigating a big JS code and in the same class I have these two functions, both using callback but I can't get which function will be called back...I guess I'm missing a basic knowledge about callbacks.. can someone explain please..

My example is general, I need to understand the idea of using callback what will be the next step, my code works I just need to understand it...

getNames(callback: (Names: name[]) => void, Name:string) {        
        Names = ['david','elton','eliza']
            callback(Names);
        });
    } 
    getFood(callback: (Foods : Food []) => void, Food:string) {        
        Foods = ['egg','bacon','chicken']
            callback(Foods);
        });
    } 
Damkulul
  • 1,406
  • 2
  • 25
  • 59
  • You could start at: [How to explain callbacks in plain english?](http://stackoverflow.com/questions/9596276/how-to-explain-callbacks-in-plain-english-how-are-they-different-from-calling-o#answer-9644980) – Ron van der Heijden Mar 01 '17 at 14:10
  • @RonvanderHeijden thanks but I already red it, didn't help me much :/ – Damkulul Mar 01 '17 at 14:11
  • `callback` in these examples is the parameter name – phuzi Mar 01 '17 at 14:11
  • @phuzi it doesn't a refer to a function ? – Damkulul Mar 01 '17 at 14:12
  • 1
    It doesn't help that this is invalid code, as the `{}` braces and `()` brackets don't match. – lonesomeday Mar 01 '17 at 14:14
  • 1
    when you call `getNames` you'll pass it a function that will act as a callback. `callback` is an arbitrary name for the parameter. – phuzi Mar 01 '17 at 14:15
  • @lonesomeday this code is just for example it dosen't have to work.. I just need to understand the Idea I dont know what is the next step where is the callback function – Damkulul Mar 01 '17 at 14:15
  • 1
    Yes it refers to the function that you'll pass in the arguments of the `getNames` or `getFood` functions. (e.g: you have a function called `yourfunction()` if you do `getNames(yourfunction,'blabla')` the argument `yourfunction`will be your callback. – Steeve Pitis Mar 01 '17 at 14:16
  • @SteevePitis ok, so if I get it right - **callback: (Names: name[]) => void** means that getNames function accept a callback function as an argument and that callback function needs to have an array of Names as an argument and to be void function. – Damkulul Mar 01 '17 at 14:19
  • 1
    Yes it's right ;) – Steeve Pitis Mar 01 '17 at 14:27
  • @SteevePitis Thank you! – Damkulul Mar 01 '17 at 14:28

0 Answers0