JavaScript Callbacks

A Javascript callback function is a function that is passed as an argument to another function and executed at a later time. The function that receives the callback function is often referred to as the “parent” function, while the function that is passed as an argument is referred to as the “callback” function.

Here is an example of a simple callback function:

				
					function greeting(name, callback) {
  console.log("Hello, " + name + "!");
  callback();
}

function farewell() {
  console.log("Goodbye!");
}

greeting("John", farewell);
				
			

In this example, the greeting function takes two arguments: a name and a callback. The greeting function prints a message and then calls the callback function. The farewell function is passed as the callback argument when the greeting function is called. When the greeting function is executed, it will print "Hello, John!" and then "Goodbye!".

Why use a Callback Function?

Javascript Callback functions are particularly useful for handling asynchronous operations. An asynchronous operation is one that does not block the execution of the rest of the program while it is running. For example, when you make a request to a server, the program does not wait for the server to respond before continuing to execute. Instead, the program continues to execute and the server’s response is handled by a callback function.

Here is an example of a callback function being used to handle an asynchronous operation:

				
					fetch("https://api.example.com/data")
  .then(response => response.json())
  .then(data => {
    console.log(data);
  });
				
			

In this example, the fetch function is used to make a request to an API. The then method is called on the returned promise, and it takes a callback function as its argument. The callback function is executed when the promise is resolved and the server’s response is received.

Synchronous & Asynchronous Callbacks

Callback functions can be either synchronous or asynchronous. A synchronous callback function is executed immediately, while an asynchronous callback function is executed at a later time.

Synchronous Callback:

				
					
function synchronousCallback(callback) {
  callback();
}

synchronousCallback(() => {
  console.log("I am a synchronous callback.");
});
				
			

Asynchronous Callback

				
					function asynchronousCallback(callback) {
  setTimeout(callback, 1000);
}

asynchronousCallback(() => {
  console.log("I am an asynchronous callback.");
});
				
			

In the above examples, the synchronousCallback function immediately calls the callback function passed to it, while the asynchronousCallback function uses the setTimeout function to execute the callback function one second later.

Error Handling

Callback functions can also be used to handle errors that occur during an asynchronous operation. The callback function is passed two arguments: the first argument is the error, and the second argument is the result of the operation. Here is an example of a callback function being used to handle an error

				
					function handleError(error, result) {
  if (error) {
    console.log("An error occurred: " + error);
  } else {
    console.log("The result is: " + result);
  }
}

fs.readFile("file.txt", "utf8", handleError);
				
			

In this example, the fs.readFile function is used to read a file. The handleError function is passed as the callback. If an error occurs while reading the file, the handleError function will be passed the error as the first argument. If the file is read successfully, the handleError function will be passed the contents of the file as the second argument.

Leave a Reply

Your email address will not be published. Required fields are marked *