Loading

Quipoin Menu

Learn • Practice • Grow

java-script / Async and Await
interview

Q1. What is Async/Await in JavaScript?
Async/Await is a modern way to handle asynchronous operations in JavaScript, built on top of Promises.

Example
async function greet() {
    return "Hello World";
}

greet().then(console.log);

async function 
return Promise automatically.


Q2. What does the async keyword do?
The async keyword is used to declare an asynchronous function. An async function always returns a Promise.

Example
async function example() {
    return "Hello World";
}

example().then(console.log);

Even though the function returns a simple string, JavaScript automatically converts it into a Promise.


Q3. What does the await keyword do?
The await keyword pauses the execution of an async function until the Promise is resolved or rejected.

Example
function getData() {
    return new Promise(resolve => {
        setTimeout(() => resolve("Data Loaded"), 2000);
    });
}

async function displayData() {
    let result = await getData();
    console.log(result);
}

displayData();

The await keyword waits for the Promise returned by getData() to complete before continuing execution.


Q4. Can await be used outside an async function?
No, await can only be used inside an async function.
If you try to use await outside an async function, it will produce an error.


Q5. Why is Async/Await better than Promises?
Async/Await is better because it improves readability and makes asynchronous code easier to write and understand.

Advantages:
  • Cleaner syntax
  • Easier error handling
  • Less complex code
  • Looks like synchronous execution