A Promise is a modern way to handle asynchronous operations in JavaScript.
It represents a value that will be available in the future — either successfully or with an error. Promises help avoid messy callback code and make programs easier to read and manage.
Promises are widely used when working with APIs, file loading, and background tasks.
Promise States
A promise can be in one of three states:
- Pending – the task is still running
- Fulfilled – the task completed successfully
- Rejected – the task failed
Understanding these states helps you control async flow clearly.
Here, a new promise is created using the Promise constructor.
The resolve function is called when the task is successful.
The reject function is called when the task fails.
The then() method runs when the promise is fulfilled.
It receives the successful result of the asynchronous task.
These examples show how promises replace complex callbacks with clean and readable code.
By understanding promises, students gain the ability to manage asynchronous operations confidently and prepare for advanced concepts like async and await.