JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print How to Print a Page Using JavaScript Textbox events in JavaScript How to find the largest number contained in a JavaScript Array?

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions JavaScript Beautifier Practice Javascript Online Object in JavaScript JavaScript Count HTML Interpreter Getters and Setters in JavaScript Throw New Error in JavaScript XOR in JavaScript Callbacks and Promises in JavaScript Atob in JavaScript Binary in JavaScript Palindrome Number in JavaScript How to Get First Character Of A String In JavaScript How to Get Image Data in JavaScript How to get URL in JavaScript JavaScript GroupBy Methods difference-between-var-let-and-const-keyword-in-javascript JavaScript Beautifier Iterating over Objects in Javascript Find the XOR of two numbers without using the XOR operator Method Chaining in JavaScript Role of Browser Object Model (BOM) in JavaScript Advanced JavaScript Interview Questions Filter() in JavaScript For Loop in JavaScript Google Maps JavaScript API Hide and Show Div in JavaScript How to Find Object Length in JavaScript Import vs. Require in JavaScript JavaScript Frontend Frameworks JavaScript Goto JavaScript Image Compression JavaScript Obfuscator JavaScript Pop() Method JavaScript replaceAll() JavaScript URL Encode JavaScript vs ReactJS JQuery Traversing Regular Expression for Name Validation in JavaScript Switch Statement in JavaScript

Callbacks and Promises in JavaScript

Introduction of Synchronous vs Asynchronous Codes

Synchronous as well as asynchronous execution of the code are two main concepts in JavaScript programming which highly depend on their quality of utilization. The term "synchronous code execution" relates to the default behaviour of JavaScript in a way that the code commands are executed one by one and follow the same order in which they are written in the code.

If the code is running synchronously, that is, operations are executed sequentially for all the operations to be completed before the next operation starts causing a blocking behaviour, and as a result, it could take the program a while to be ready to continue. For example, consider the following synchronous code snippet:

Javascript

console.log("Start");

console.log("Middle");

console.log("End");

The code results in Start, Middle and End, each in a new line in the same order they are written in the code(similar to their order in the respective code).

For example, consider an asynchronous operation like fetching data from an API. Let an API call be an operation that is conducted asynchronously. Such calls return sequenced data.

Javascript

console.log("Start");

fetch("https://api.example.com/data")

  .then(response=>response.json()).then(data=>console.log(data))

.catch(error => console.error("Error:", error));

console.log("End");

console.log("End");

The situation here will be considered the unique spot where the "fetching" word included in the endless loop code will get started right at the beginning. The run of the program occurs as the fetch phase; that is, the receiving of the instruction by the memory storage does not happen at the same time. The remote data will be fetched after successful data fetching specifies that the .then() method will handle the response. Meanwhile, data from computing devices is also displayed on the console.

Importance of Asynchronous Programming in JavaScript

  1. Non-blocking Behavior: One of the main features of asynchronous operations in JavaScript is that the code's execution is not interrupted by the task on the heap. This means that the program will always be receptive, especially when processing time-consuming activities such as network requests and file I/O.

2. Improved Performance: While this aspect helps to optimize the execution of several tasks concurrently, thereby also leveraging the benefits of performance and resource utilization, asynchronous programming can be vital. In contrast to the synchronized version of code that waits for one operation to finish before initiating the other, asynchronous code can start several operations simultaneously and make superior use of these resources.

3. Enhanced User Experience: Asynchronous programming is the backbone of interacting with the user in many ways. In dealing with cross-thread interaction, such as taking data from a server or processing user inputs, JavaScript web applications can legitimately maintain a well-functioning user experience without pausing or becoming unresponsive.

4. Concurrency: Asynchronous programming makes it possible for JavaScript to manage tasks concurrently. Thus, coders can write code capable of processing complicated operations simultaneously. Concurrency enables faster execution and scalable, which is very relevant in applications that deal with high request capacity or big data sets.

Definition and Purpose of Callback Functions

Callback functions are an essential concept in JavaScript. They are often used to handle asynchronous tasks and execute code after certain events occur. A callback function is simply a function that is passed as an argument to another function and invoked or called back at a later time, typically when an asynchronous operation completes, or a certain condition is met.

The primary role of callback functions is that of non-blocking/asynchronous programming, allowing tasks to be executed without having to be connected to the main flow of the program. If a callback function is passed into an asynchronous function, it will not remain halted but rather continue to execute while the operation is in progress. When the operation finishes, the callback function is called. The program can manipulate data or do other things afterwards.

Callback functions usually appear when handling events, timers, AJAX requests, and other asynchronous operations in JavaScript. They allow the user to review the result of asynchronous work without hindering the code's execution, thereby improving the program's speed and efficiency.

How Callback Functions are Used to Handle Asynchronous Operations

Asynchronous operations in JavaScript are handled by the callback functions, which are an integral part of it. Synchronous tasks, such as fetching data from an adjacent server or reading a file from disk, often take time to complete, during which the program executes all other tasks instead of waiting for the operation to finish. Callback functions are, in fact, a means of inviting developers to consider what should be done once the asynchronous mechanism is completed.

Developers run the provided callback function upon the completion of asynchronous task invocation. This callback function performs the role of a return value handler and is dedicated to the operation's result. In this case, the operation finishes; therefore, the callback function receives the result or error that stopped them.

Let's say data is requested through AJAX to a server. A callback function is supplied as an argument, for example, fetch() or the XMLHttpRequest function. This callback function displays how data should be processed or actioned when the information is obtained. Furthermore, callback functions could be used when using timers or event listeners to dictate the response to the expiration of the timer or the occurrence of the event.

Callback hell: nested and difficult-to-maintain code

In effect, callback hell, or "pyramid of doom," is the concept of the usage of multiple nested callback functions that are used for solving the asynchronous operations in the JavaScript code. It causes the creation of messy code that is hard to read and follow and needs to be better structured due to the presence of callback functions and the nested nature of the code.

The equivalent of callback hell contains the synchronization item for each function, which is received and passed as an argument to an operation that was declared previously, i.e., the one before. The deeper in the code that additional operations are inserted, the more the code becomes nested. The coder follows the flow of execution and debugs issues and difficulties that emerge. This nesting, therefore, often results in mistakes that may be hard to notice and correct, such as brackets that need to be included or misaligned.

Example: The following example illustrates callback hell:

asyncOperation1((result1) => {

asyncOperation2(result1, (result2) => {

asyncOperation3(result2, (result3) => {

// More nested callbacks...

});

});

});

The callback hell problem has led developers to employ some techniques, including modularization, named functions, and promises.

  1. Modularization: Use modularity and reusable smaller granular units, as well as several asynchronous functions. This technique increases usability and supportability by separating similar tasks into separate functions.

2. Named Functions: Create functions named after the actions to be performed instead of using anonymous functions written inline. By giving the various functions their names, the program becomes more feasible for others to read and debug.

3. Promises: The virtues of promises are that they give a cleaner, and more structured way to deal with the functions of general form compared to callback functions. Promises are carried by events that signal the finishing of an asynchronous operation, or nothing happens. We can then pass them further to the then() and catch() methods to handle then and catch cases, respectively. Callback promises to alleviate callback hell in the sense it smooth the flattening of the code structure and provides more effective error handling.

Examples:

Example 1: Event Handling with Callbacks:

// Implement function to simulate asynchronously doing something.

function simulateAsyncOperation(callback) {

setTimeout(() => {

callback("Data fetched successfully");

}, 2000);

}

// State an implementation of a callback function which can be utilized for handling the asynchronous result.

function handleResult(result) {

console.log("Result:", result);

}

// invoke the async method with the callback function being the given parameter.

simulateAsyncOperation(handle result);

Explanation: Here, we have a function named simulateAsyncOperation, which executes a setTimeout function that simulates an asynchronous operation. We pass this async function (as a parameter) to the method handleResult (of type callback function). When the async operation finishes, it calls the callback function, which in turn forwards the result.

Example 2: Array Iteration with Callbacks:

const numbers = [1, 2, 3, 4, 5];

 //Declare a callback function.

function squareNumber(num) {

return num * num;

}

// apply javascript maps method to call functions to all the elements

const squaredNumbers = numbers.map(squareNumber);

console.log("Squared numbers:", squaredNumbers);

Explanation: We define a squareNumber function that takes a number as input and returns that number squared. To compensate for this, we will apply the map method, which will then pass a callback function to every element of the array, returning a new array with a set of squared numbers.

Example 3: File System Operations with Callbacks (Node. js):

const fs = require('fs');

fs.readFile('example.txt', 'utf8', error => data) {

if (err) {

console.error("Error reading file:", err);

return;

}

console.log("File content:", data);

});

Explanation: In this case (Node.js Environment), the fs.readFile function is deployed asynchronously to read the contents of the file. We give it a callback function as an argument for returning the result of the operation. On the occasion of an error in file reading, this callback function is programmed to deal with it.

Definition and Characteristics of Promises

Promises are objects in JavaScript that represent the eventual completion or failure of an asynchronous operation, allowing for more structured and readable asynchronous code. They have several key characteristics: They have several key characteristics:

  1. State: A promise can have one of the following three states: pending, verified, or rejected. Initially, the state of an awaiting promise is pending, which indicates that the async operation it represents is not over yet. In the case of a successful operation, the promise moves on to the state of fulfilled, thus pointing to the fact that the operation finished well. Alternatively, when the operation succeeds, the promise switches to the fulfilled state.

2. Immutable State: The point is that as soon as the promise's object settles (i.e., its fulfilment or rejection), the promise's status cannot be changed. This unchangeability implies that asynchronous computing cannot be affected by any outside factors that might make the result unpredictable or random.

3. Chaining: Promises supports method chaining These consecutive linking can make handling asynchronous dependencies simple, thereby improving code readability, which most developers prefer.

4. Error Handling: Promises to help developers maintain structured code without redundant error handling, using the .catch() casting function. Errors that may happen by the dimensional processes within the promise chaining can be queued up and traced; therefore instead of terminating the program by making the app crash, they only stop the propagation of the errors upwards.

5. Composition: Promises can be orchestrated to work together by using .then() and .catch(). This way, complicated asynchronous workflows can be created using more simple and reusable entities. Composability facilitated the code reuse and modularization processes, and as a result, the codebases became more manageable and scalable.

6. Asynchronous Execution: Promise introduction is Non-blocking Asynchronization so that JavaScript code can perform time-consuming operations without disturbing the main execution thread (including data fetching from the server or reading files from disk). Such an elect asynchronous ability allows web applications to be fast and responsive.

Advantages of Promises over Callbacks

  1. Readability and Maintainability: Promises are awesome because they steer developers to clean and concise interfaces by removing the nested, cluttered callback functions. With a mixture of chains, asynchronous operations can be linked, and more appropriately, the .then () method can be used to understand and manage code.

2. Error Handling: Promises offer possible error handling mechanisms through the method .catch(), which enables the smooth display of any issue as it occurs in the chain of promises. Doing so makes it easier to distinguish between error management and callbacks, as the error-handling logic in the practical world is often tangled within the callback functions.

3. Composition: Promises' suspension assures repetition, which not only realizes operations but also facilitates that tasks are properly arranged to pass through the next state. The output of this means of interoperability allows the programmers to create complicated asynchronous sequences about things they do by putting smaller and reusable works together in a chained line of promise-based functions.

4. Unidirectional Data Flow: Promises control of a one-way data flow, which is either requests sent from one operation or none at all. This assists in avoiding action or side effects and enables one to reason easily about the data flow in asynchronous code.

5. Error Propagation: Error handlers immediately above a catch console point to the nearest Error handler, meaning error handling is done from one location only. While errors in async code may be explicit or implicit, however, they require either the callback parameters or a customized error handling mechanism.

6. Promise. all() and Promise.race(): Although they have a limited set of utility methods like Promise. all() or Promise. race(), they serve the purpose of operating on multiple promises concurrently. Such approaches not only increase the process's performance but also remove the difficulties of organizing and synchronizing asynchronous tasks, which may be very hard using callbacks.

7. Built-in Timeout Handling: Promises can be tailored using the race() method and timeout logic. So, developers can customize promises using the race() method and timeout logic.

Syntax for Creating and Returning Promises

Creating Promises:

Promises are created using the Promised constructor, which takes a function in the argument. This task, the executor function, tasks get called up as soon as the promise is created. More often of this, the operation that the promise represents is an asynchronous one. Within the executor function, you have the option of invoking the resolve() function to close the promise with an assigned value or the reject() function to reject the promise with a reason (generally an error).

javascript

const promise = new Promise((resolve, rejection) => {

// Asynchronous operation

setTimeout(() => {

// Fulfill the promise

resolve("Operation completed successfully");

// reject(new Error("Operation failed"));

}, 1000);

});

Returning Promises:

However, promises are usually used as they act like methods to define different forms of asynchronous functions. Within the area, give the new mint and keep the transactions that are taking place in the ASYNC function. The error occurs when we resolve the promise using the resolve() function or reject it using the reject() function.

javascript

function asyncTask() {

return new Promise((resolve, reject) => {

// Asynchronous operation

setTimeout(() => {

// Fulfill the promise

resolve("Operation completed successfully");

// Here stands a promise-making method called a promise procedure promise.

// reject(new Error("Operation failed"));

}, 1000);

});

}

asyncTask().then((result) => {

console.log("Promise fulfilled:", result);

}).catch((error) => {

console.error("Promise rejected:", error.message);

});

Chaining promises with then() and catch() Methods

While asynchronous operations have many features, we observe that task handling can be achieved using JavaScript promises. One of their main attributes is that they are synchronized together and that there are two chains: Then () and catch(). This makes the process more rigorous in terms of the code, as well, as it is considered to be structured and warranted with the respected syntax.

Using then() Method:

In then(), the promise is omitted, and consequently, chaining is allowed. It takes two optional callback functions as arguments: the author sets the first function dealing with proceeding (onFulfilled) and the second one dealing with rejection (onRejected). However, another group of them can return either a value or a promise itself, which is often the case.

javascript

asyncTask()

.then((result) => {

// As we fulfill the first one,

console.log("First promise fulfilled:", result);

// Let the output either be a new value or a promise, the latter being a guarantee.

return anotherAsyncTask();

})

.then((result) => {

console.log("Second promise fulfilled:", result);

})

.catch((error) => {

// Correct or rectify any errors that are on the board in the distributed ledger.

console.error("Error occurred:", error.message);

});

Using catch() Method:

The catch() method is used to catch and serialize any errors that happen in the promise chain. It's a single callback function that is passed. Take it so that if any of the promises in the chain are rejected, it will be called.

javascript

asyncTask()

.then((result) => {

console.log("Promise fulfilled:", result);

})

.catch((error) => {

// What to do in case the chain is not working properly is also covered.

Console.error("Error occurred:", error.message);

});

Chaining Example:

Here's an example that demonstrates chaining promises with then() and catch() methods:

javascript

getUserData()

  .then((userData) => {

    console.log("User data fetched:", userData);

    return fetchUserPosts(userData.id);

  })

  .then((userPosts) => {

    console.log("User posts fetched:", userPosts);

    return fetchPostComments(userPosts[0].id);

  })

  .then((postComments) => {

    console.log("Post comments fetched:", postComments);

  })

  .catch((error) => {

    console.error("Error occurred:", error.message);

  });

In this example, each then() call fulfils a promise, while the catch() method handles any errors that occur in the chain. This allows for a clean and structured way to handle asynchronous operations and their potential errors.

Combining Promises with Async/Await

Async/await is a special case of modern JavaScript syntax that allows you to write async as if you had any type that you can work with. It extends the existing utilities and arranges a more readable and concise syntax for operations related to the manipulation of the executed code lines. In this part, we're going to compare affirmations with await for the so-called asynchronous programming.

Using Async/Await with Promises:

The async functions in JavaScript utilize the await keyword, which pauses the execution of code until the promise is fulfilled. This consecution makes asynchronism code act like synchronism and easy to read. Through the incorporation of the async/await feature, promises can be chained to make the code clearer and easier to maintain.

javascript

async function fetchData() {

try {

const data = await fetch('https:∕api.example.com/data');

const jsonData = await data.json() ;

console.log('Fetched data:', jsonData);

return jsonData;

} catch (error) {

console.error('Error fetching data:', error);

throw error;

}

}

With this, the fetchData function goes for the async function, and you can use the await keyword from it. The await keyword is used, saying the waiting by the resolving of the promises returned by fetch and json methods. This, in turn, leads to code and the sorts of code that are more legible in terms of embedded then() call methods.

Error Handling with Async/Await:

Async/await will facilitate the simpler control flow of the error handling block by having the try/catch blocks surround the asynchronous operations that are likely to be executed without errors. Like a promise with a catch handler function for error handling, everything is quite systematic and usually easier to interpret.

javascript

async function fetchData() {

try {

const data = await fetch ('https://api.example.com/data');

const jsonData = await data.json();

console.log('Fetched data:', jsonData);

return jsonData;

} catch (error) {

console.error('Error fetching data:', error);

throw error;

}

}

In the above example, the catch block captures any errors that occur during the execution of the async function, allowing you to handle them gracefully.

Combining Async/Await with Promise.all:

Async/await can also be combined with Promise. All () to concurrently execute multiple asynchronous operations and wait for all of them to complete.

javascript

async function fetchUserData() {

  const [userData, userPosts] = await Promise.all([

  fetchUserData(),

  fetchUserPosts(),

  ]);

  console.log('User data:', userData);

console.log('User posts:', userPosts);

}

In this example, fetchUserData() and fetchUserPosts() are called concurrently using Promise. all(), and the await keyword is used to wait for both promises to resolve.

Comparison between Callbacks and Promises:

Callbacks and promises are two methods commonly used to manage asynchronous observations in JavaScript. Although both perform the same function, they have different attributes and usage modes. In the current section, callbacks vs. promises are discussed in terms of syntax, error handling, readability, and scalability issues.

Syntax:

Callbacks:

This kind of callback contains passing the function as an argument, and while the other function executes it, that execution occurs after the asynchronous operation finishes.

javascript

function fetchData(callback) {

setTimeout(() => {

callback(null, 'Data fetched successfully');

}, 1000);

}

fetchData((error, data) => {

if (error) {

console.error('Error:', error);

} else {

console.log('Data:', data);

}

});

Promises:

Promises provide a cleaner and more structured syntax for handling asynchronous operations. They represent a value that may be available now, in the future, or never.

javascript

function fetchData() {

return new Promise((resolve, reject) => {

setTimeout(() => {

resolve('Data fetched successfully');

}, 1000);

});

}

fetchData().then((data) => {

console.log('Data:', data);

}).catch((error) => {

console.error('Error:', error);

});

Error Handling:

Callbacks:

Callbacks can greatly enhance the speed and efficiency of an application's codebase when compared to traditional event loops. However, error handling in callbacks can result in the dreaded callback hell, meaning that multiple nested callbacks create a codebase that is almost unreadable and impossible to maintain.

javascript

function fetchData(callback) {

setTimeout(() => {

(if Math.random()<0.5) {

callback(null, 'Data fetched successfully');

} else {

callback(new Error('Error occurred, failed to fetch data!'));

}

}, 1000);

}

Promises:

These promises will use methods such as .then() and .catch() to treat errors more cleanly for easier reading, maintaining, and even updating.

javascript

function fetchData() {

return new Promise((resolve, reject) => {

setTimeout(() => {

if(Math.random() < 0.5){

resolve('Data fetched successfully');

} else {

reject (new Error ('The operation of fetching data has failed'));

}

}, 1000);

});

}

Readability:

Callbacks:

If the number of callbacks is nested, we end up with callback hell. Some programmers may also have problems reading that kind of code and maintaining it.

javascript

fetchData((error, data) => {

if (error) {

console.error('Error:', error);

} else {

processData =(data, (error, result)) => {

if (error) {

console.error('Error:', error);

} else {

displayResult(result, (error) => {

if (error) {

console.error('Error:', error);

}

});

}

});

}

});

Promises:

Promises allow us to deal with asynchronous operations more linearly and readably (especially when composing several operations; traditional approaches to this problem do not allow this).

javascript

fetchData()

.then((data) => processData(data))

.then((result) => displayResult(result))

.catch((error) => console.error('Error:', error));

Scalability:

Callbacks:

Callbacks may become troublesome to handle, and as the application continues to scale with complex new features, the code will become systemic, resulting in callback hell and spaghetti code.

Promises:

Promises are easier to reuse, and instant updates are performed using promises rather than callbacks. Scalability and maintainability make it easy to structure the code and manage asynchronous operations, including handling errors.

Best Practices for Promises and Callbacks in JavaScript:

  1. Use Promises for Asynchronous Operations: Promises are perfect when handling tasks that should take time to complete, like fetching data from a server or reading files. Like promises, asynchronous programming will appear high-level, as the syntax will be cleaner and more readable than callbacks.

2. Handle Errors Appropriately: In order to prevent possible problems, always treat errors in Promises and callbacks impeccably. In Promises, use the catch() method to catch and deal with any Promises that reject the Promise. For callbacks, add on the first argument of an error and check on the callbacks function if there is any error.

3. Prefer Promises over Callbacks: Using Promises over traditional callbacks is one of the best practices that you should always use wherever possible. Promises allow us to study multiple mistakes definitively, follow the code step by step, and so on, leading to more maintainable and understandable code.

4. Utilize Async/Await for Asynchronous Operations: Take advantage of async/await logic, which was first introduced in ECMAScript 2017, to employ Promises in a way that looks like the one we are used to in the synchronous world. To summarize, this code streamlines asynchronous operations by specifying what to do synchronously, followed by what to accomplish later, which gives the impression of running sequentially in nature.

5. Keep Callback Functions Small and Focused: When using callbacks, ensure that they are small and have definite tasks. This eases code recognition and contributes to the sense of code debugging and maintenance.

6. Use Named Functions for Callbacks: Whenever possible, use named functions instead of anonymous functions for callbacks. Named functions enhance code readability, promote code reusability, and make it easier to debug.

7. Promisify Callback-Based APIs: Transform old-style callback-based APIs to promise-based ones using implemented utilities such as util. Promising one node.js and personal wrapper functions. This gives rise to a uniform system to manage deferred and concurrent operations.

Conclusion

In the end, both callbacks and promises are foundational techniques for the asynchronous operations that JavaScript uses. Callbacks, optimization: although they are the most used functional approach, they can lead to callback hell, and the code is quite difficult to read and maintain.

Nonetheless, pledges give you a more firm and well-documented structural way with inbuilt error-catching and chain operations. They explicitly express that the way to deal with asynchronous pieces of code can afford code organization and scalability. But callbacks are still very common, especially in old javascript.

Nowadays, a promise is the most often applied design as it offers better clarity, simplicity, and extended usage options. Switching to the mode of promises over callbacks can bring the desired effects of javascript applications in a high degree of efficiency, maintainability, and good scalability qualities.

← Prev Next →