The three states of a Promise in JavaScript—pending, fulfilled, and rejected—represent different stages in the lifecycle of the asynchronous operation the Promise represents. Here's an explanation of each state:
Pending:
This is the initial state of a Promise when it is created.
In the pending state, the asynchronous operation associated with the Promise has not yet completed.
Promises start in the pending state and can transition to either the fulfilled or rejected state.
Fulfilled:
The Promise transitions to the fulfilled state when the asynchronous operation successfully completes.
When a Promise is fulfilled, it means that the operation it represents has finished successfully, and it may carry a resolved value (e.g., the result of a successful API request).
You can access the resolved value using the .then() method.
Rejected:
The Promise transitions to the rejected state when the asynchronous operation encounters an error or fails.
In the rejected state, the Promise contains information about the reason for the failure, typically an error message or an error object.
You can handle errors and access the rejection reason using the .catch() method.
Here's a visual representation of how a Promise can transition between these states:
Initial State: [Pending]
|
V
Successful: [Fulfilled]
|
V
Error/Failure: [Rejected]
In summary, the pending state represents the ongoing asynchronous operation, fulfilled represents a successful completion with a result value, and rejected represents an error or failure with an associated reason. Promises provide a structured way to work with asynchronous code and handle different outcomes, making it easier to manage asynchronous flows and error handling in JavaScript.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The three states of a Promise in JavaScript—pending, fulfilled, and rejected—represent different stages in the lifecycle of the asynchronous operation the Promise represents. Here's an explanation of each state:
Pending:
Fulfilled:
Rejected:
Here's a visual representation of how a Promise can transition between these states:
In summary, the pending state represents the ongoing asynchronous operation, fulfilled represents a successful completion with a result value, and rejected represents an error or failure with an associated reason. Promises provide a structured way to work with asynchronous code and handle different outcomes, making it easier to manage asynchronous flows and error handling in JavaScript.