JavaScript Promises Explained: A Startup Analogy for Beginners
Source: Dev.to
What is a Promise
A promise is an asynchronous operation in JavaScript that represents whether the operation is successful or not.
Think of it like building a startup feature: you start the work now, but the result (success or failure) will come later.
Asynchronous operation – a task that runs in the background and allows other code to execute without waiting for it to finish.
Why We Need Promises
- To handle asynchronous tasks properly
- To manage success or failure clearly
- To write cleaner and more readable code
- To make code more structured
- To work efficiently with APIs and background tasks
States of a Promise
There are three possible states:
- Pending – the default state; the task has started but the result is not yet known.
- Resolved – the promise completed successfully and returns a result.
- Rejected – the promise failed and returns an error, like when something doesn’t go as planned.
Creating and Handling a Promise
Creating a Promise
Creating a promise is like starting a new project: you decide to build something but you don’t know the outcome yet.
const promise = new Promise((resolve, reject) => {
// asynchronous work here
});
Handling a Promise
After launching the project you wait for the result: if it succeeds you celebrate, if it fails you fix it. In JavaScript, promises are handled with .then(), .catch(), and .finally():
const promise = new Promise((resolve, reject) => {
// Example: resolve or reject immediately
resolve("Promise Resolved");
// reject("Promise Rejected");
});
promise
.then((data) => {
console.log(data); // runs on resolve
})
.catch((error) => {
console.log(error); // runs on reject
})
.finally(() => {
console.log("code ends"); // always runs
});
.then()runs when the promise is resolved..catch()runs when the promise is rejected..finally()runs regardless of the outcome.
Promise Methods
These static methods are called directly on the Promise class.
Promise.resolve()
Returns a promise that is already resolved.
Analogy: a client instantly approves your proposal.
const resolved = Promise.resolve("Already resolved");
Promise.reject()
Returns a promise that is already rejected with an error.
Analogy: a manager instantly rejects your leave request.
const rejected = Promise.reject(new Error("Already rejected"));
Promise.any()
Takes an array of promises and resolves with the first promise that fulfills. If all promises reject, it rejects.
Analogy: pitching to multiple investors; you need just one “yes”.
Promise.any([p1, p2, p3])
.then((value) => console.log("First fulfilled:", value))
.catch((err) => console.log("All rejected:", err));
Promise.all()
Waits for all promises to fulfill; if any reject, the whole thing rejects.
Analogy: all departments must finish their work before launch.
Promise.all([p1, p2, p3])
.then((values) => console.log("All fulfilled:", values))
.catch((err) => console.log("One failed:", err));
Promise.allSettled()
Waits for all promises to settle (either fulfilled or rejected) and returns an array describing each outcome.
Analogy: you get a full report from every department, regardless of success.
Promise.allSettled([p1, p2, p3])
.then((results) => console.log(results));
Promise.race()
Returns a promise that settles as soon as any of the input promises settles (whether fulfilled or rejected).
Analogy: the first investor to respond decides your direction.
Promise.race([p1, p2, p3])
.then((value) => console.log("First settled:", value))
.catch((err) => console.log("First rejected:", err));
Conclusion
JavaScript promises provide a structured way to handle tasks that don’t finish instantly. From creating a promise to handling it with .then(), .catch(), .finally(), and using static methods like Promise.all(), Promise.any(), and others, promises make asynchronous code cleaner, more readable, and easier to manage.