Asynchronous JavaScript Pt.2— async/await

Rahul Chowdhury
3 min readMar 7, 2023

Asynchronous programming is a vital aspect of modern web development, and JavaScript’s async/await syntax offers a concise and intuitive way to handle asynchronous operations. With async/await, you can write asynchronous code that looks and behaves like synchronous code, making it easier to understand and reason about.

What is Asynchronous Programming?

Asynchronous programming is a programming paradigm that allows code to continue running while waiting for a long-running operation to complete. In traditional synchronous programming, a program must wait for each operation to complete before moving on to the next one. This can result in a slow and unresponsive application, especially when dealing with network requests or file I/O.

With asynchronous programming, a program can start an operation and then move on to other tasks while the operation completes in the background. When the operation finishes, the program can pick up where it left off and continue processing the results.

Promises

In JavaScript, asynchronous operations are typically handled using promises. Promises provide a sophisticated way to handle asynchronous operations. A promise represents a value that may not be available yet but will be at some point in the future. Promises can be in one of three states: pending, resolved, or rejected. When a promise resolves or rejects, it calls a corresponding function that can handle the result.

To know more on Promises, you can refer to my blog about Promises.

https://medium.com/@rahulchowdhury_69408/asynchronus-javascript-pt-1-promise-f9d6b62deaef

Async/Await

async/await is a syntactic sugar on top of promises that makes working with asynchronous code much more intuitive. It allows you to write asynchronous code that looks and behaves like synchronous code, making it easier to understand and reason about.

The async keyword is used to define an asynchronous function. An asynchronous function returns a promise that resolves to the function's return value. Within an asynchronous function, you can use the await keyword to pause the function until an asynchronous operation completes. When the operation completes, the value it resolves to is returned by the await expression.

Here’s an example of an asynchronous function that fetches data from an API:

async function fetchData() {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const data = await response.json();
return data;
}

In this example, the fetch function is called to fetch data from an API. The await keyword is used to pause the function until the data is returned. Once the data is returned, it is parsed as JSON and returned by the function.

Error Handling in async/await:

Error handling in asynchronous programming with async/await involves handling any errors that occur during the execution of asynchronous operations. When using async/await, you can handle errors using try/catch blocks.

Here’s an example:

async function fetchData() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const data = await response.json();
console.log(data);
} catch (error) {
console.log(error);
}
}

fetchData();

In the example above, the fetchData function uses async/await to make an asynchronous HTTP request to retrieve JSON data from a remote API. The try block contains the asynchronous operations, which are the two await statements that retrieve and parse the data. If an error occurs during either of these operations, the code jumps to the catch block, which logs the error to the console.

Conclusion

async/await is a modern feature in JavaScript that simplifies asynchronous programming by allowing developers to write asynchronous code in a synchronous style. It was introduced in ES2017 and is now widely supported in modern web browsers and Node.js.

That’s all from these blog about Javascript async/await. Please hit the 👏 icon as much as possible, and if this has helped you gain a little more knowledge, make sure to Follow.

--

--

Rahul Chowdhury

A professional Salesforce Developer with 2+ years of experience and a freelancer in ReactJs and React Native for more than 3 years.