---
title: "What is the purpose of async and await in JavaScript? Provide an example."  
description: "What is the purpose of async and await in JavaScript? Provide an example."  
author: "Ravi Vishwakarma"  
published: 2024-06-17  
updated: 2024-06-17  
canonical: https://www.mindstick.com/interview/33911/what-is-the-purpose-of-async-and-await-in-javascript-provide-an-example  
category: "javascript"  
tags: ["web development", "javascript", "front-end development"]  
reading_time: 2 minutes  

---

# What is the purpose of async and await in JavaScript? Provide an example.

`async` and `await` are used to handle **asynchronous** operations more **synchronously**. `async` marks a function as **asynchronous**, and `await` pauses the function execution until the promise resolves.

## Example:

```javascript
<script type="text/javascript">
	async function fetchData() {
	  try {
	    let response = await fetch('https://jsonplaceholder.typicode.com/users');
	    let data = await response.json();
	    console.log(data);
	  } catch (error) {
	    console.error(error);
	  }
	}
	fetchData();
</script>
```

## Read more

[**Describe the difference between == and === operators in JavaScript.**](https://www.mindstick.com/interview/33909/describe-the-difference-between-and-operators-in-javascript)

[**What is a closure in JavaScript? Provide an example.**](https://www.mindstick.com/interview/33908/what-is-a-closure-in-javascript-provide-an-example)

[**What is the difference between let, const, and var when declaring variables in JavaScript?**](https://www.mindstick.com/interview/33906/what-is-the-difference-between-let-const-and-var-when-declaring-variables-in-javascript)

[**Explain the concept of "hoisting" in JS. How does it affect variable and function declarations?**](https://www.mindstick.com/interview/33907/explain-the-concept-of-hoisting-in-js-how-does-it-affect-variable-and-function-declarations)

## Answers

### Answer by Ravi Vishwakarma

`async` and `await` are used to handle **asynchronous** operations more **synchronously**. `async` marks a function as **asynchronous**, and `await` pauses the function execution until the promise resolves.

## Example:

```javascript
<script type="text/javascript">
	async function fetchData() {
	  try {
	    let response = await fetch('https://jsonplaceholder.typicode.com/users');
	    let data = await response.json();
	    console.log(data);
	  } catch (error) {
	    console.error(error);
	  }
	}
	fetchData();
</script>
```

## Read more

[**Describe the difference between == and === operators in JavaScript.**](https://www.mindstick.com/interview/33909/describe-the-difference-between-and-operators-in-javascript)

[**What is a closure in JavaScript? Provide an example.**](https://www.mindstick.com/interview/33908/what-is-a-closure-in-javascript-provide-an-example)

[**What is the difference between let, const, and var when declaring variables in JavaScript?**](https://www.mindstick.com/interview/33906/what-is-the-difference-between-let-const-and-var-when-declaring-variables-in-javascript)

[**Explain the concept of "hoisting" in JS. How does it affect variable and function declarations?**](https://www.mindstick.com/interview/33907/explain-the-concept-of-hoisting-in-js-how-does-it-affect-variable-and-function-declarations)


---

Original Source: https://www.mindstick.com/interview/33911/what-is-the-purpose-of-async-and-await-in-javascript-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
