Async and Await in JavaScript (original) (raw)

Last Updated : 2 May, 2026

Async/Await in JavaScript allows you to write asynchronous code in a clean, synchronous-like manner, making it easier to read, understand, and maintain while working with promises.

async function fetchData() { try { // Simulated API response (mock data) const response = await Promise.resolve({ json: async () => ({ userId: 1, id: 1, title: "Sample Post", body: "This is mock data for async/await demonstration" }) });

const data = await response.json();
console.log(data);

} catch (error) { console.error("Error fetching data:", error); } }

fetchData();

`

Syntax:

async function functionName() {
try {
const result = await someAsyncFunction();
console.log(result);
} catch (error) {
console.error("Error:", error.message);
}
}

Async Function

Async functions let us write promise-based code in a synchronous-looking way, improving readability while keeping the execution non-blocking. An async function always returns a Promise—if it returns a non-promise value, JavaScript automatically wraps it in a resolved Promise.

**Syntax:

async function myFunction() {
  return "Hello";
}

javascript `

const getData = async () => { let data = "Hello World"; return data; }

getData().then(data => console.log(data));

`

Await Keyword

The await keyword pauses the execution of an async function until a Promise is resolved or rejected. It can only be used inside an async function, making asynchronous code easier to read and manage.

const getData = async () => { let y = await Promise.resolve("Hello World"); console.log(y); }

console.log(1); getData(); console.log(2);

`

Error Handling in Async/Await

Errors in async/await are handled using try...catch instead of resolve and reject.

async function fetchData() { try { let response = await fetch('https://api.example.com/data'); let data = await response.json(); console.log(data); } catch (error) { console.error('Error fetching data:', error); } }

`