Skip to content

Latest commit

 

History

History
78 lines (51 loc) · 3.14 KB

Callback.md

File metadata and controls

78 lines (51 loc) · 3.14 KB

Callbacks in JavaScript: A "Vikram Vedha" Tale 🎬

Introduction to Callbacks

In the dynamic world of JavaScript programming, callbacks play a pivotal role, akin to the intricate plot twists in the Tamil movie "Vikram Vedha" (2017). As we embark on this journey, let's delve deeper into the significance of callbacks and explore real-world scenarios where they become indispensable.

Why Callbacks Are Needed

Callbacks function as the directors of our code, orchestrating sequences of tasks much like directors Pushkar and Gayathri shaped the gripping narrative in "Vikram Vedha." They are vital for handling asynchronous operations, events, and ensuring that specific tasks are executed only after the completion of others.

Real-world Scenarios: Callbacks in Action

Consider scenarios where callbacks shine:

  • Handling HTTP Requests: Callbacks are commonly used to manage asynchronous tasks, such as fetching data from an API. The callback ensures that subsequent actions are executed only when the data is successfully retrieved.

  • User Interactions on a Web Page: Callbacks come into play when responding to user interactions, like button clicks. They help manage the flow of actions, ensuring a seamless user experience.

Callback Example: A Detective's Call

function solveCase(suspect, callback) {
  // Detective work happening here

  // Once the case is solved, make the callback
  callback(`Case Solved: ${suspect}`);
}

function celebrateSuccess(result) {
  console.log(result);
  console.log("Celebrating success!");
}

// Calling the solveCase function with a callback
solveCase("Vedha", celebrateSuccess);

Output:

Case Solved: Vedha
Celebrating success!

Explanation: In this example, the solveCase function represents a detective solving a case. Once the case is solved, the callback function (celebrateSuccess) is invoked to celebrate the success.

Limitations of Callbacks: A Crime Thriller Twist

Callbacks, while indispensable, can lead to a phenomenon known as the "Callback Hell" or "Pyrrhic Callback," reminiscent of the suspenseful and complex plot twists in "Vikram Vedha." When multiple callbacks are nested within each other, the code structure becomes convoluted and challenging to maintain.

Callback Hell Example: Unraveling the Plot

detective.solveCase("Vikram", function (result) {
  console.log(result);

  crimeBoss.getPunishment("Vedha", function (punishment) {
    console.log(punishment);

    detective.reportToSuperior("Vikram", function (report) {
      console.log(report);

      // More callbacks nested here...

    });
  });
});

Explanation: In this callback hell scenario, callbacks are nested within each other, leading to a pyramid of indentation and making the code difficult to follow—much like the intricate plot twists in "Vikram Vedha."

The Road Ahead: Teasing Promises and Async/Await

Understanding callbacks and their challenges is crucial, but fear not! In the subsequent sections, we'll explore solutions to escape the callback hell. Get ready for a code narrative inspired by the suspenseful storytelling of "Vikram Vedha"! 🕵️‍♂️🎥