Understanding The 3 States Of A Javascript Promise: Pending Fulfilled Rejected

JavaScript Promises are a powerful tool introduced in ES6 to handle asynchronous operations. They operate in three states and understanding these states is crucial to leveraging the full potential of Promises. These states guide the lifecycle of a Promise, defining how it behaves over time. Let's delve into these states, their meanings, and how they interact to make Promises such a valuable addition to JavaScript.

JavaScript Promise Tutorial: Resolve, Reject, and Chaining in JS and ES6
JavaScript Promise Tutorial: Resolve, Reject, and Chaining in JS and ES6

At their core, Promises are used to handle operations that might not complete immediately, such as network requests or file system operations. They provide a cleaner and more efficient way to manage asynchronous operations compared to callbacks. The three states of a Promise - Pending, Fulfilled, and Rejected - map onto the possible outcomes of these asynchronous operations.

Promises
Promises

Pending State

The Pending state is the initial state of a Promise. When a Promise is created, it's in this state. It means the asynchronous operation has not yet completed. In other words, the Promise is still waiting for the result of the operation it was created to handle.

In Which State Can a Promise Be?
In Which State Can a Promise Be?

A Promise can remain in the Pending state indefinitely if the associated asynchronous operation never completes. This is an important aspect to consider when working with Promises, as it ensures that the code continues to run even while the Promise is waiting for its operation to finish.

Transition to Fulfilled or Rejected State

Promise Intro in JavaScript
Promise Intro in JavaScript

The Pending state is transient. Once the asynchronous operation finishes, the Promise transitions to either the Fulfilled or Rejected state. The result of the operation determines which state it transitions to. If the operation was successful, the Promise transitions to the Fulfilled state. If an error occurred, it transitions to the Rejected state.

This transition is what makes Promises so powerful. Unlike callbacks, Promises don't halt the execution of the code when waiting for a result. Instead, they allow the rest of the code to continue running until the operation is complete, at which point they handle the result appropriately.

Fulfilled State

a white screen with some type of programming code on it's back side and the words asynchronous code everywhere in java script
a white screen with some type of programming code on it's back side and the words asynchronous code everywhere in java script

The Fulfilled state indicates that the asynchronous operation was successful. This is also known as a 'resolved' state. Once a Promise is in this state, it remains there indefinitely. This state is typically represented by the 'resolve' value passed to the Promise's executor function.

When a Promise is in the Fulfilled state, it can be 'chain-reacted' using methods like 'then'. This means that you can execute other actions or operations based on the resolved value, and return another Promise if necessary. This is a key aspect of the power of Promises, allowing for a chaining of asynchronous operations.

Accessing the Fulfilled Value

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

The value of the Fulfilled state can be accessed using .then() method. This method takes a callback function as an argument, which is executed when the Promise transitions to the Fulfilled state. This function receives one argument, the resolved value of the Promise.

The value of the Fulfilled state can also be read using .catch() method if the Promise was chained from another Promise that was rejected. However, .catch() should preferably be used alongside .then() to handle possible rejections in the Fulfilled chain.

⭐️🎀 JavaScript Visualized: Promises & Async/Await
⭐️🎀 JavaScript Visualized: Promises & Async/Await
an image of a diagram that shows the process to create a web page for a website
an image of a diagram that shows the process to create a web page for a website
JavaScript Reserved Words: What You Need to Know 🚀
JavaScript Reserved Words: What You Need to Know 🚀
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
10 Reasons to Learn JavaScript.
10 Reasons to Learn JavaScript.
📘 JavaScript Basics| Linking JS File to HTML | Chapter 2 📖 Part 2
📘 JavaScript Basics| Linking JS File to HTML | Chapter 2 📖 Part 2
JavaScript Higher Order Functions - 1
JavaScript Higher Order Functions - 1
Full-Stack Web Development | ChatGPT & Interview Tips on Instagram: "📌 Link in the bio to Download Front-End Developer Interview Kit . . . JavaScript Object Methods  . . . #javascript #js #angularjs #reactjs #angular #web #webdeveloper #html #css #css3 #html5 #frontend #frontenddeveloper #vuejs #expressjs #nodejs #coder #coding #programmer #programming #software #informationtechnology #java #python #php #frontendjob #frontend #chatgpt #chatgptprompts #prompts #chatgptforprogramming" Css Javascript Integration, Programming Foundations With Javascript, Javascript Object, Full Stack Javascript, Flow-based Programming For Javascript, Full Stack Javascript Developer, Javascript Debugging Console Output, Javascript Substring Example, Javascript Chrome Profiler
Full-Stack Web Development | ChatGPT & Interview Tips on Instagram: "📌 Link in the bio to Download Front-End Developer Interview Kit . . . JavaScript Object Methods . . . #javascript #js #angularjs #reactjs #angular #web #webdeveloper #html #css #css3 #html5 #frontend #frontenddeveloper #vuejs #expressjs #nodejs #coder #coding #programmer #programming #software #informationtechnology #java #python #php #frontendjob #frontend #chatgpt #chatgptprompts #prompts #chatgptforprogramming" Css Javascript Integration, Programming Foundations With Javascript, Javascript Object, Full Stack Javascript, Flow-based Programming For Javascript, Full Stack Javascript Developer, Javascript Debugging Console Output, Javascript Substring Example, Javascript Chrome Profiler
How to Declare Variables in JavaScript
How to Declare Variables in JavaScript
JavaScript Fundamental
JavaScript Fundamental
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
a computer screen with the words css basics on it
a computer screen with the words css basics on it
JavaScript Recursion Simplified
JavaScript Recursion Simplified
two screens showing the same number of words in each word, and one with an arrow pointing
two screens showing the same number of words in each word, and one with an arrow pointing
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
How to Create a Function in JavaScript: Step-by-Step Guide 🚀
How to Create a Function in JavaScript: Step-by-Step Guide 🚀
JavaScript Literals & Data Types: The Building Blocks of Code 🔢
JavaScript Literals & Data Types: The Building Blocks of Code 🔢
15 JavaScript Array Methods You Should Master Today
15 JavaScript Array Methods You Should Master Today
JavaScript vs typescript
JavaScript vs typescript
Synchronous & Asynchronous Javascript
Synchronous & Asynchronous Javascript

Rejected State

The Rejected state indicates that the asynchronous operation failed, typically due to an error. Once a Promise is in this state, it remains there indefinitely. This state is typically represented by the 'reject' value passed to the Promise's executor function.

When a Promise is in the Rejected state, it can be 'caught' using the .catch() method. This method takes a callback function as an argument, which is executed when the Promise transitions to the Rejected state. This function receives one argument, the reason for the Promise's rejection, which is usually an error.

The .catch() method is crucial for error handling in Promise-based code. It ensures that errors don't break the execution of the rest of the code, but are instead handled gracefully.

,.catch() Method

The .catch() method can be used anywhere in the Promise chain, not just after the Promise that was rejected. This is because a Promise in a chain can be rejected even if it's not the Promise that was initially rejected.

It's also important to note that .catch() doesn't prevent the error from being logged in the console. It only prevents the Promise chain from breaking due to an unhandled error. If you want to suppress the console log, you need to handle the error yourself in the .catch() function.

In conclusion, understanding the three states of a JavaScript Promise - Pending, Fulfilled, and Rejected - is key to leveraging Promises for handling asynchronous operations. Regardless of the outcome, Promises allow code to continue executing while they wait for their operation to complete, providing a cleaner, more error-prone way to deal with async tasks in JavaScript.

Related Articles

String() In Js Faceless Youtube Channel Tutorial Queryselector Examples Full Claude Tutorial Youtube Best Youtube Tutorial For Excel W3schools Document Queryselectorall Youtube Claude Cowork Tutorial Crochet Star Blanket Tutorial Youtube Claude Code Cli Tutorial Youtube Youtube Makeup Tutorial Smokey Eye