Engineer's Tutorial
Aug 15, 20245 min read
JavaScript Promises - A begineer's guide
NY

Nishi Yadav

Mern stack developer

Table of contents

Have you ever found yourself waiting for something—maybe a friend to text you back or a package to arrive? In the world of JavaScript, dealing with these kinds of "waiting" situations is crucial, especially when your code needs to fetch data, wait for user input, or interact with a server. This is where JavaScript promises come into play!

If you're new to programming or JavaScript, don't worry. By the end of this post, you'll not only understand what promises are but also how to use them to make your code cleaner, easier to read, and more efficient. Let's dive in! 🚀

 

What Exactly Is a Promise?

Imagine you're ordering a pizza. You call the pizza place, and they promise to deliver it within 30 minutes. This promise can end in one of three ways:

Pending: The pizza is still being made (the promise hasn't been fulfilled yet).
Fulfilled: The pizza arrives on time, and you're happy (the promise is kept).
Rejected: Something goes wrong, and the pizza never arrives (the promise is broken).

In JavaScript, a promise works similarly. It's an object that represents a task that will be completed in the future—either successfully (fulfilled) or unsuccessfully (rejected).

Here's a simple promise:

const pizzaPromise = new Promise((resolve, reject) => {
 let pizzaArrives = true;
 if (pizzaArrives) {
   resolve("Pizza has arrived! 🍕");
 } else {
   reject("No pizza today... 😞");
 }
});

In this code, the promise will either resolve (the pizza arrives) or reject (something went wrong).


 

Why Do We Need Promises?

Before promises, JavaScript developers used callbacks to handle asynchronous tasks. However, as projects grew more complex, callbacks became harder to manage and led to what's known as "callback hell"—a tangled mess of nested functions that are difficult to read and debug.

Promises solve this problem by making your code look more like the logical sequence of steps you want to perform. They also help you avoid the messy, nested structure of callbacks.

 

How Do You Use Promises?

Let’s break down the key parts of a promise:

`.then()`: This method runs when the promise is fulfilled.
`.catch()`: This method runs when the promise is rejected.
`.finally()`: This method runs no matter what, whether the promise is fulfilled or rejected.

Imagine ordering your pizza again:

pizzaPromise
 .then((message) => {
   console.log(message); // "Pizza has arrived! 🍕"
 })
 .catch((error) => {
   console.error(error); // "No pizza today... 😞"
 })
 .finally(() => {
   console.log("Pizza promise settled."); // Always runs
 });

 

Chaining Promises for More Power

Promises become even more powerful when you chain them. This means you can string together several `.then()` methods to handle multiple asynchronous tasks in a sequence.

Let’s say you want to place an order, track the delivery, and then eat the pizza. You can chain promises like this:

placeOrder()
 .then((orderDetails) => trackDelivery(orderDetails))
 .then((deliveryStatus) => eatPizza(deliveryStatus))
 .then(() => console.log("Enjoying my pizza! 😋"))
 .catch((error) => console.error("Something went wrong:", error));

In this scenario, each step depends on the previous one. If at any point the process fails, the `.catch()` method will handle the error.


 

Handling Multiple Promises at Once

Sometimes, you need to deal with multiple promises at the same time. JavaScript offers two powerful methods for this:

  • `Promise.all()`: Waits for all promises to resolve and returns an array of results. If any promise is rejected, the whole operation fails.
Promise.all([orderPizza(), getDrinks(), setTable()])
 .then((results) => {
   console.log("All set for the party! 🎉", results);
 })
 .catch((error) => {
   console.error("One of the tasks failed:", error);
 });

 

  • `Promise.race()`: Returns the result of the first promise that resolves, ignoring the rest.
Promise.race([fastPizza(), slowPizza()])
 .then((result) => {
   console.log("First pizza is here! 🍕", result);
 })
 .catch((error) => {
   console.error("All pizzas failed... 😞", error);
 });

 

JavaScript Promises Summary

States:
  • Pending: The initial state, neither fulfilled nor rejected.
  • Fulfilled: The operation completed successfully.
  • Rejected: The operation failed.
Creating a Promise
const myPromise = new Promise((resolve, reject) => {
 // Perform an async task
 const success = true;
 if (success) {
   resolve("Task completed successfully!");
 } else {
   reject("Task failed.");
 }
});

 

Handling Promises
  • `.then()`: Executes when the promise is fulfilled.
  • `.catch()`: Executes when the promise is rejected.
  • `.finally()`: Executes regardless of the promise being fulfilled or rejected.
myPromise
 .then(result => {
   console.log(result); // "Task completed successfully!"
 })
 .catch(error => {
   console.error(error); // "Task failed."
 })
 .finally(() => {
   console.log("Promise settled.");
 });
 
Chaining Promises

Promises can be chained to perform sequential asynchronous operations.

fetchData()
 .then(data => processData(data))
 .then(processedData => saveData(processedData))
 .then(() => console.log("All steps completed successfully."))
 .catch(error => console.error("An error occurred:", error));

 

Working with Multiple Promises

`Promise.all()`: Waits for all promises to resolve or for any to reject.

Promise.all([promise1, promise2, promise3])
   .then(results => {
     console.log("All promises resolved:", results);
   })
   .catch(error => {
     console.error("One or more promises failed:", error);
   });

 

`Promise.race()`: Resolves or rejects as soon as one of the promises in the array resolves or rejects.

 Promise.race([promise1, promise2, promise3])
   .then(result => {
     console.log("First promise resolved:", result);
   })
   .catch(error => {
     console.error("First promise rejected:", error);
   });

 

Common Methods and Properties

`Promise.resolve(value)`: Returns a promise that resolves with the given value.

const promise = Promise.resolve("Resolved value");

 

`Promise.reject(error)`: Returns a promise that rejects with the given error.

const promise = Promise.reject("Error occurred");

 

`Promise.allSettled()`: Waits for all promises to settle (either fulfilled or rejected) and returns an array of objects describing the outcome.

 Promise.allSettled([promise1, promise2, promise3])
   .then(results => {
     results.forEach(result => console.log(result.status));
   });

 

Async/Await (Built on Promises)
  • Async functions: Use `async` to define a function that returns a promise.
  • Await: Use `await` to pause the execution of an async function until a promise is resolved.
async function fetchData() {
 try {
   const data = await fetch('https://api.example.com/data');
   const result = await data.json();
   console.log(result);
 } catch (error) {
   console.error("Error fetching data:", error);
 } finally {
   console.log("Fetch attempt finished.");
 }
}

 

Key Points to Remember

  • Promises are essential for handling asynchronous operations in a more readable and maintainable way.
  • Use `.then()` for chaining, `.catch()` for error handling, and `.finally()` for cleanup tasks.
  • `Promise.all()` and `Promise.race()` are powerful for handling multiple promises concurrently.
  • For modern, cleaner syntax, use `async/await` to work with promises.
     

Wrapping Up: Why Promises Are Your Best Friend in JavaScript 🤝

Promises are like that reliable friend who always has your back when things get tough. They help you manage complex asynchronous tasks in a way that's readable, maintainable, and logical.

By mastering promises, you'll be well-equipped to handle everything from simple delays to complex operations involving multiple asynchronous tasks. Whether you're building a simple app or working on a large-scale project, promises are an essential tool in your JavaScript toolkit.

So next time you find yourself "waiting" in your code—whether for a pizza, a data fetch, or anything else—remember that JavaScript promises are here to help. Start using them today, and watch your code become more powerful and easier to manage!


Happy coding! 🚀