The Promise Object: A Placeholder for Future Values

A Promise is an IOU for a value from an async operation, like a network request. It's a placeholder that will eventually hold a result or an error. Use it for fetching data or reading files. The footgun: always add a .catch() to handle failures.
Why it exists
JavaScript is single-threaded. To avoid freezing the user interface while waiting for slow operations like network requests, we need a way to manage tasks that complete later. Promises were created to make handling these asynchronous operations cleaner and more predictable than nested callbacks, often called "callback hell".
The mental model
A Promise is an object that acts as a placeholder for the eventual result of an asynchronous operation. Imagine ordering a coffee: you get a ticket (the Promise) right away. You don't have the coffee yet, so the ticket's state is 'pending'. Later, the barista calls your number and gives you the coffee (the Promise is 'fulfilled') or tells you they're out of beans (it's 'rejected'). The ticket guarantees you'll eventually get an outcome.
How it works
A Promise starts in a 'pending' state. The asynchronous operation it represents will eventually finish. If it succeeds, the Promise transitions to the 'fulfilled' state with a resulting value. If it fails, it transitions to the 'rejected' state with an error. You attach handlers using the .then() method for the fulfilled case and the .catch() method for the rejected case. A Promise that is either fulfilled or rejected is considered 'settled'.
When to use it
Use Promises whenever you perform an operation that doesn't complete immediately. This is standard for modern web APIs like fetch() for making HTTP requests, reading files in Node.js, or interacting with browser APIs like Geolocation. They are the building blocks for the even cleaner async/await syntax.
When not to use it
Avoid wrapping synchronous code in a Promise unless you are intentionally deferring its execution or conforming to an API that expects a Promise. For simple, immediate calculations, using a Promise adds unnecessary complexity. If an operation can be done instantly without blocking, just return the value directly.
One canonical example
The fetch API returns a Promise that resolves to the HTTP response. You chain .then() calls to process the successful result and .catch() to handle any errors in the process.
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Fetch failed:', error));This code requests data, parses the JSON body, and logs the result, with a safety net for failures.
Interview question
What is the primary purpose of using a JavaScript Promise?
- a.To immediately return a value from a function without waiting for any operations to finish.
- b.To convert any synchronous function into an asynchronous one.
- c.To ensure that a block of code is executed only after the user has interacted with the page.
- d.To manage the eventual result or error of an operation that completes at an unknown future time.Correct
Why? this is the answer
The card defines a Promise as a "placeholder for the eventual result of an asynchronous operation" and states they are used for tasks that "don't complete immediately." Option B is a common misconception; Promises manage already asynchronous tasks, they don't inherently make synchronous code non-blocking.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #javascript
- #asynchronous
- #web-api
- #promise
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on javascript — each one lists the topics its interview covers.
See open roles