JavaScript Interview Question #79

What are the states of a Promise?

Promises, Async/Await, Fetch & Modern JavaScript Mid-Level Intermediate

Quick Interview Answer

A Promise starts as pending. It becomes fulfilled when the operation succeeds and resolve is used, or rejected when the operation fails and reject is used.

Detailed Explanation

A Promise starts as pending. It becomes fulfilled when the operation succeeds and resolve is used, or rejected when the operation fails and reject is used.

Fulfilled and rejected Promises are collectively described as settled. Once a Promise is settled, later attempts to resolve or reject it do not change its state.

Code Example

const promise = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve({ id: 1, name: "Asif" });
    }, 500);
});

promise.then(user => console.log(user));