JavaScript Callback Function In Hindi

📔 : Java Script 🔗

Callback Functions वो function होते हैं जो किसी दूसरे function में as an arguments pass किये जाते हैं। और जिस function में ये pass किये जाते हैं उन्हें Higher - Order Function कहते हैं।


जैसा कि आप पहले भी पढ़ चुके होंगे कि JavaScript में Functions first class Object होते हैं। Means , इन्हे as a constructor भी use किया जा सकता है।

Need Of Callback Function In JS

हम जानते हैं कि JavaScript Synchronous Scripting language है , means code Top -Down order में run होता है। But कई cases में code Asynchronously भी run होता है।


Actually जब हम setTimeOut() या await() function का use करके Asynchronously Code run करते हैं , तो वो code of block run होने से पहले ही उससे आगे का Code execute होने लगता है। जिसकी वजह से program में जभी कभी errors भी आ जाती हैं।


But Callback Function का use करके उन errors से बच सकते हैं , क्योंकि जिस function के लिए callback function as an argument pass किया जाता है , उस function के completely execute होने के बाद ही callback function run होते हैं।

JS Callback Function Example

well Callback Functions को अच्छे से समझने के लिए एक simple example देखतें हैं।

File : js_callback_fun.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Callback Function Example</title>
  </head>
  <body>
    <script type="text/javascript">
      /* define a function */
      function print_name(name)
      {
        document.write(`Your full name is : ${name}`);
      }

      function enter_name(callback)
      {
        let name = prompt('Enter you full name.');
        callback(name);
      }
      /* now call the function and pass the function name as an argument */
      enter_name(print_name);
    </script>
  </body>
</html>

Where prompt() function , Window class / object का एक method है जिसे हम globally access कर सकते हैं।



ऊपर दिए गए example में एक print_name() function को enter_name() function में as an argument pass किया गया है , तो enter_name() function जब तक completely execute नहीं हो जायेगा तब तक print_name() execute नहीं होगा। इसमें Running flow Top - Down है इस तरह के function को Synchronous Callback Function कहा जाता है।

enter_name(function print_name(name)
{
   document.write(`Your full name is : ${name}`);
});
/*It will also produce same OP*/

JS Asynchronous Callback Function

Asynchronous means अगर JavaScript Script run होने के लिए wait करना पड़ रहा है तो rest of code (उससे आगे का code) run हो जायेगा।

File : js_asyn_callb_fun.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Asynchronous Callback Function Example</title>
  </head>
  <body>
    <script type="text/javascript">
      setTimeout(function(){
        console.log('Run after 1 seconds');
      }, 1000);
      console.log('Normal Code');
    </script>
  </body>
</html>
Output
//Output in Console
Normal Code
Run after 1 seconds

setTimeout() किसी pass किये गए function को specified time के बाद run करता है , दिया जाने वाला time milliseconds में होता है।

1000 ms = 1 second

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers