2

So I understand that callbacks are essentially functions that are passed into another function and executed at a later time.

I am also aware that a lot of major libraries such as jQuery use callbacks efficiently, but I wanted a simple code example along with an explanation on when it is best to use a callback function to solve a problem. All the examples I ve seen so far are the setTimeout, but I wanted to see a fresh example to broaden my mind on how callback functions can be used.

So utimately, I wanted to see 1. An example of a callback function. 2. Why it would be an appropriate solution/approach in that example.
3. Possibly other common reasons/scenarios for using callbacks.

Thank you very much in advance!

J. Lee
  • 513
  • 4
  • 15
  • 2
    `other built in functions` - that's a broad stroke there. If the examples you've seen aren't helping, I doubt very much another example will help – Jaromanda X Oct 08 '15 at 08:18
  • Hmmm good point. Thank you for pointing that out. Let me take that out of the question. I just wanted to see good examples of callback functions and why it would be appropriate to use it in that context. – J. Lee Oct 08 '15 at 08:21
  • You can read up a bit on `Functional Programming` and how in JS a function can be passed as an argument to another function – Sandeep Nayak Oct 08 '15 at 08:30
  • Will be sure to do it. thanks! – J. Lee Oct 08 '15 at 08:37

3 Answers3

2

A callback is usually used when you have to deal with a function (or method) which you don't know how much time could take to compute.

For example, assume you need to get some water from a shaft to do something, but you don't actually know where and how far this shaft is from your house: you ask a friend (asynchronous function) to kindly walk to the shaft and take the water for you, so that when he comes back you can use it.

In the mean time, while waiting for the water, you could have done some other useful things.

A basic example of that would be:

function getWater(callback) {
    // Walk to the shaft
    // Take water
    // Go back home 
    // This function is asynchronous

    callback();
}

function useWater() {
    // Do what you needed to do with water
}

getWater(useWater);
// While waiting to use the water:
doUsefulThings();
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Thank you for the concise explanation. I think I now have a better understanding of when to use callback functions. I guess its a matter of reading and writing more code, as well as reading up on what others have to say about this topic. Anyways, much appreciated! – J. Lee Oct 08 '15 at 08:36
0

functions are first-class objects, we can pass a function as an argument in another function and later execute that passed-in function or even return it to be executed later. This is the essence of using callback functions in JavaScrip JAVA SCRIPT callbacks

Example:

   function test(data,function(datafromfunc){
           //my function
});

inside service function

  function(data,callback)
{
  //your logic
  callback(result)
}

example 2 //JavaScript callback functions

function randomize(min, max, callback) {  
    var random = Math.ceil(Math.random() * (min - max) + max);
    callback(random);
}

randomize(5, 15, function(rndm) {
    alert(rndm);
});
Arun
  • 1,177
  • 9
  • 15
  • check this http://stackoverflow.com/questions/7070495/why-use-callback-in-javascript-what-are-its-advantages – Arun Oct 08 '15 at 08:34
  • Thank you for the example code and a very easy to understand example :) – J. Lee Oct 08 '15 at 08:36
  • Although I do appreciate your answer, I felt like Marco Bonelli's answer provided more clarity. If I could, I would accept both your answers as correct. – J. Lee Oct 08 '15 at 08:41
  • thank you,if you know more,please share – Arun Oct 08 '15 at 08:41
0

In JavaScript, we have the ability to treat Functions just like any other variables.

That is, traditionally we write a function like:

function sayHello(name){
      console.log("Hello "+name);
}

and then invoke it passing the necessary arguments:

sayHello("JavaScript");  

This is pretty much how we use functions in other scripting languages as well.

But, what if I tell you, that in JavaScript, a function is as good as a variable in a way that it can be passed as an argument when we invoke the function?

For example:

var sayHello = function(name){  // My callback method
  console.log(name);
};

function getNameAndSayHello(callback){
   var name = 'My name is a Sandeep'; // I do some other things here...
   //..........some other code.............
   //..... when i am done with other things, i call the callback method... 
   callback(name);

}

getNameAndSayHello(sayHello);

As you will see, I am passing sayHello(a function) as an argument to another function getNameAndSayHello. In short, essence of Functional Programming, although not in the purest form.

This ability to pass functions just like any other variable/value makes it very convenient for us to invoke the callback at a later time which suits us.

http://jsfiddle.net/wz9b1s8m/

Sandeep Nayak
  • 4,649
  • 1
  • 22
  • 33