WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

JavaScript Β· Intermediate Β· question 23 of 100

What are promises, and how do they work in JavaScript?

πŸ“• Buy this interview preparation book: 100 JavaScript questions & answers β€” PDF + EPUB for $5

In JavaScript, a Promise is an object that represents a value that may not be available yet, but will be resolved at some point in the future. Promises are a way to handle asynchronous code, which means code that does not run in a predictable order.

Promises have three states: pending, fulfilled, and rejected. When a Promise is first created, it is in the pending state. It will stay in this state until it is either fulfilled (resolved) or rejected (failed). Once a Promise is resolved, it cannot be changed to a different state.

Here’s an example of a Promise:

    const promise = new Promise((resolve, reject) => {
        setTimeout(() => {
            const value = Math.random();
            if (value >= 0.5) {
                resolve(value);
            } else {
                reject("Value is less than 0.5");
            }
        }, 1000);
    });
    
    promise
    .then((result) => {
        console.log("Promise resolved with value:", result);
    })
    .catch((error) => {
        console.error("Promise rejected with error:", error);
    });

In this example, a new Promise is created with a function that takes two parameters: resolve and reject. The resolve function is used to fulfill the Promise with a value, while the reject function is used to reject the Promise with an error message. In this case, the Promise resolves with a random value greater than or equal to 0.5 after a delay of 1000ms.

The Promise is then chained with a .then() method to handle the case when the Promise is fulfilled (resolved), and a .catch() method to handle the case when the Promise is rejected. If the Promise is resolved, the .then() method is called with the resolved value. If the Promise is rejected, the .catch() method is called with the rejection reason.

Promises are useful in JavaScript because they allow you to write code that runs asynchronously in a way that is easy to read and maintain. They also help to avoid callback hell, a situation where multiple nested callbacks can make code difficult to read and debug.

In summary, Promises are objects in JavaScript that represent a value that may not be available yet, but will be resolved at some point in the future. They have three states: pending, fulfilled, and rejected. Promises are useful for handling asynchronous code in a way that is easy to read and maintain.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview β€” then scores it.
πŸ“ž Practice JavaScript β€” free 15 min
πŸ“• Buy this interview preparation book: 100 JavaScript questions & answers β€” PDF + EPUB for $5

All 100 JavaScript questions Β· All topics