JavaScript callback

 

A callback function can be defined as a function passed into another function as a parameter. Don't relate the callback with the keyword, as the callback is just a name of an argument that is passed to a function.

In other words, we can say that a function passed to another function as an argument is referred to as a callback function. The callback function runs after the completion of the outer function. It is useful to develop an asynchronous JavaScript code.

In JavaScript, a callback is easier to create. That is, we simply have to pass the callback function as a parameter to another function and call it right after the completion of the task. Callbacks are mainly used to handle the asynchronous operations such as the registering event listeners, fetching or inserting some data into/from the file, and many more.

Now, let's see how to create a callback by using some illustrations.

It is an example of an asynchronous callback. Asynchronicity can be defined as if JavaScript has to wait to complete the operation and execute the rest of the program during waiting.

Example1

In this example, there are two functions getData( x, y, callback) and showData(). Here, we are calling the getData() with the showData(); that is, we are passing it as the third argument of the getData() function along with two parameters. As a result, the getData() is invoked with the specified parameters, including the callback.

The getData() function display the multiplication of two numbers and once it gets completed the callback function will get executed. In the output, we can see the data of the showData() function gets printed after the output of getData() function.


Code


  1. <html>  
  2. <head>  
  3. <style>  
  4. </style>  
  5. </head>  
  6. <body>  
  7. <h1> Hello World :) :) </h1>  
  8. <h3> The getData() function is called its arguments and the callback is executed after the completion of getData() function. </h3>  
  9. <script>  
  10. function getData(x, y, callback){  
  11. document.write(" The multiplication of the numbers " + x + " and " + y + " is: " + (x*y) + "<br><br>" );  
  12. callback();  
  13. }  
  14. function showData(){  
  15. document.write(' This is the showData() method execute after the completion of getData() method.');  
  16. }  
  17. getData(20, 30, showData);  
  18.   
  19. </script>  
  20. </body>  
  21.   
  22. </html>


Callbacks are generally used to continue the execution after completing an asynchronous operation - such are referred to as the asynchronous callbacks.

Now, in the next example, we will see a callback which gets immediately executed


Example2

It is another example of using callbacks. It is an example of synchronous callback that gets immediately executed.

Here, there are two functions getData(callback), which takes the input from the user using the prompt box, and the function showData(name, amt), which displays the data entered by the user using the alert dialog box.


Code

  1. <html>  
  2. <head>  
  3.   
  4. </head>  
  5. <body>  
  6. <h1> Hello World :) :) </h1>  
  7. <h2> This is the javaTpoint.com </h2>  
  8. <script>  
  9. function showData(name, amt) {  
  10. alert(' Hello ' + name + '\n Your entered amount is ' + amt);  
  11. }  
  12.   
  13. function getData(callback) {  
  14. var name = prompt(" Welcome to the javaTpoint.com \n What is your name?");  
  15. var amt = prompt(" Enter some amount...");  
  16. callback(name, amt);  
  17. }  
  18.   
  19. getData(showData);  
  20. </script>  
  21. </body>  
  22.   
  23. </html>



0 Comments