Promises become truly useful when you handle their results correctly. JavaScript provides two main methods for this: then() and catch().
The then() method is used when a promise is successful, and the catch() method is used when something goes wrong.
Using then() and catch() makes asynchronous code clean, readable, and easy to debug.
How then() and catch() Work
- then() runs when the promise is fulfilled
- catch() runs when the promise is rejected
- They help separate success logic from error logic
- They make async code easier to understand
- They are used in almost every modern JavaScript project
Together, they form the backbone of promise handling.
This promise simulates a task that may succeed or fail.
Now we will handle its result using then() and catch().
The then() method runs when the promise is fulfilled.
It receives the success message and executes the related code.
These examples show how then() and catch() handle success and failure separately.
By mastering then() and catch(), students gain the ability to manage asynchronous tasks confidently and build stable, professional JavaScript applications.