JavaScript Interview Questions

23 Questions
JavaScript

JavaScript

Web DevelopmentFrontendBackend

Question 14

What are async functions in JavaScript?

Answer:

Async functions in JavaScript are functions that operate asynchronously via the event loop, using an implicit promise to return their result. These functions allow the use of the 'await' keyword to pause execution until a promise is fulfilled, making asynchronous code easier to read and write. Async functions simplify working with promises and provide a cleaner syntax for handling asynchronous operations.

Here is an example demonstrating the use of async functions:

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);
    }
}

fetchData();

In this example, fetchData is an async function that waits for the fetch operation to complete and then parses the JSON response. The try...catch block is used to handle any errors that occur during the asynchronous operations.

Recent job openings