DEV Community

Ankit Dagar
Ankit Dagar

Posted on

Asynchronous-JavaScript

1. Callbacks :

Callbacks are a fundamental concept in JavaScript that allow for asynchronous programming and handling of events. A callback is simply a function that is passed as an argument to another function and is invoked or called at a later time, often when a certain event or asynchronous operation completes.

The primary purpose of using callbacks is to ensure that certain code is executed only when a particular task is finished or an event occurs. This is especially useful in situations where the result of an operation is not immediately available or when the order of execution needs to be controlled.

Here's an example to illustrate the usage of callbacks:

function fetchData(callback) {
  // Simulating an asynchronous operation
  setTimeout(() => {
    const data = { message: 'Data successfully fetched' };
    callback(data); // Invoke the callback function with the fetched data
  }, 2000);
}

function processData(data) {
  console.log('Processing data:', data.message);
}

fetchData(processData); // Pass processData function as a callback
Enter fullscreen mode Exit fullscreen mode

In the above example, we have a fetchData() function that simulates an asynchronous operation, such as fetching data from a server. It takes a callback function as an argument. Inside fetchData(), we use setTimeout() to simulate a delay of 2 seconds before the data is fetched.

Once the data is fetched, we invoke the callback function passed to fetchData() and provide the fetched data as an argument. In this case, the callback function is processData().

When fetchData() is called, it starts the asynchronous operation and registers the processData() function as the callback. After 2 seconds, the fetched data is passed to processData(), and it executes the code within its body, logging the message "Processing data: Data successfully fetched" to the console.

Callbacks are powerful because they allow us to control the flow of execution in asynchronous scenarios and perform tasks based on the completion of asynchronous operations or events. They are widely used in JavaScript for handling events, making AJAX requests, working with timers, and many other asynchronous operations.

2. Promises :

Promises are a built-in feature in JavaScript that provide a more structured and intuitive way to handle asynchronous operations. They are objects representing the eventual completion (or failure) of an asynchronous operation and allow you to handle the result or error using a chain of methods.

Promises have three states:

  1. Pending: The initial state when a promise is created. It represents that the asynchronous operation is still ongoing, and the promise is neither fulfilled nor rejected.
  2. Fulfilled: The state when the asynchronous operation is successfully completed, and the promise is fulfilled. At this point, the promise holds a value or result.
  3. Rejected: The state when the asynchronous operation encounters an error or fails. The promise is rejected and holds a reason or error message.

Here's an example of using promises:

function fetchData() {
  return new Promise((resolve, reject) => {
    // Simulating an asynchronous operation
    setTimeout(() => {
      const data = { message: 'Data successfully fetched' };
      resolve(data); // Resolve the promise with the fetched data
    }, 2000);
  });
}

function processData(data) {
  console.log('Processing data:', data.message);
}

fetchData()
  .then(processData) // Process the fetched data
  .catch(error => {
    console.error('Error:', error);
  }); 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have a fetchData() function that returns a promise. Inside the promise's executor function, we simulate an asynchronous operation using setTimeout(). After 2 seconds, the promise is fulfilled by calling resolve() and passing the fetched data.

We can use the then() method on the promise to specify the callback function that should be executed when the promise is fulfilled. In this case, we pass processData as the callback function. The then() method returns a new promise, allowing for method chaining.

If an error occurs during the asynchronous operation, we can handle it using the catch() method. The catch() method takes a callback function that is executed when the promise is rejected, allowing us to handle any errors that occurred during the execution.

Promises provide a more structured approach to handling asynchronous operations compared to callbacks. They simplify error handling, allow for better code organization, and support chaining multiple asynchronous operations together. Promises have become a standard way of handling asynchronous code and are widely supported in modern JavaScript environments.

3. Code Control :

Code control refers to the ability to manage and control the flow of execution in a program. In JavaScript, there are several mechanisms and constructs that allow for code control, including conditional statements, loops, and error handling.

1.Conditional Statements:

Conditional statements, such as if, else if, and else, enable you to control the execution of code based on specified conditions.
Example:

if (condition) {
  // Code to execute if the condition is true
} else if (anotherCondition) {
  // Code to execute if the anotherCondition is true
} else {
  // Code to execute if none of the conditions are true
}
Enter fullscreen mode Exit fullscreen mode

2.Loops:

Loops allow you to repeatedly execute a block of code until a certain condition is met.
Common loop constructs in JavaScript are for, while, and do...while.
Example:

// for loop
for (let i = 0; i < 5; i++) {
  // Code to execute in each iteration
}

// while loop
while (condition) {
  // Code to execute as long as the condition is true
}

// do...while loop
do {
  // Code to execute at least once, then repeat as long as the condition is true
} while (condition);
Enter fullscreen mode Exit fullscreen mode

3.Error Handling:

Error handling allows you to catch and handle exceptions or errors that occur during program execution.
The try...catch statement is used to surround a block of code that may potentially throw an exception, and allows for handling of the error.
Example:

try {
  // Code that might throw an exception
} catch (error) {
  // Code to handle the error
} finally {
  // Code that will execute regardless of whether an error occurred or not
}
Enter fullscreen mode Exit fullscreen mode

4.Control Flow:

Control flow refers to the order in which statements and code blocks are executed.
Control flow can be influenced using conditional statements, loops, and control flow keywords like break, continue, and return.
Example:

 // break statement
for (let i = 0; i < 5; i++) {
  if (i === 3) {
    break; // Exit the loop when i is 3
  }
  console.log(i);
}

// continue statement
for (let i = 0; i < 5; i++) {
  if (i === 3) {
    continue; // Skip the rest of the loop body when i is 3
  }
  console.log(i);
}

// return statement
function calculateSum(a, b) {
  return a + b; // Return the sum of a and b and exit the function.
}
Enter fullscreen mode Exit fullscreen mode

By utilizing these code control mechanisms, you can determine the execution path of your program, handle errors gracefully, and repeat or skip certain sections of code based on conditions. Understanding and effectively utilizing code control constructs is essential for writing structured and efficient JavaScript code.

Top comments (0)