Intermediate Topic 19 of 51 Web Development
Promises
Objects representing eventual completion of an async operation.
How it works
A promise is an object representing the eventual result of an asynchronous operation. It is in one of three states: pending, fulfilled with a value or rejected with an error, and once settled it does not change. then registers success handlers, catch handles errors and finally runs cleanup; each returns a new promise, enabling chaining and flattening. Combinators include Promise.all, allSettled, race and any.
Step-by-step walkthrough
- A promise starts pending.
- On success it becomes fulfilled with a value.
- On failure it becomes rejected with an error.
- then handles the value and catch handles the error.
- Promises chain, and async/await is syntax over them.
Where it is used
APIs such as fetch, file operations, database drivers and many libraries return promises, which compose cleanly with async and await.
Common pitfall
Forgetting to return a promise inside then breaks the chain. Unhandled rejections can crash Node.js processes or go unnoticed in browsers. Creating a promise with the constructor around code that already returns a promise, the explicit construction anti-pattern, adds bugs. Promises start immediately, so they are not lazy. Always handle errors at the end of chains and use Promise.all for independent work.
Where it fits in the course
Promises is topic 19 of 51 in Web Development, rated Intermediate. It follows Asynchronous JavaScript. The next topic is Event Loop.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Promises?
Where do people use Promises?
What is the most common mistake with Promises?
What should I learn before this topic?
How can I practice it?
Is Promises hard to learn?
How long does it take to learn Promises?
Which topics are related to Promises?
What should I study after Promises?
Related topics
- Asynchronous JavaScript (Web Development)
- Event Loop (Web Development)
- Browser DevTools (Web Development)
- ES Modules (Web Development)
- Futures and Promises Model (Concurrency & Parallelism)