Promises in ES6

Enhance Your Asynchronous JavaScript Code

Patrick Karsh
3 min readApr 17, 2023
I promise…

In the realm of ECMAScript 6 (ES6), Promises emerge as a formidable solution for tackling asynchronous operations within JavaScript. By offering a methodical and streamlined alternative to callbacks, notorious for spawning the so-called “callback hell,” Promises serve as a welcome respite for developers navigating the complexities of asynchronous coding.

States of a Promise

A Promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. It can be in one of three states:

Pending: The initial state; the promise is neither fulfilled nor rejected.

Fulfilled: The asynchronous operation completed successfully, and the promise has a resulting value.

Rejected: The asynchronous operation failed, and the promise has a reason for the failure.

Note that broken is not a state of a promise.

Promise Methods

Promises have two main methods:

then(): This method is used to attach callbacks that will be called when the promise is fulfilled. It takes two optional arguments: a function to be called when the promise is fulfilled, and another function to be called when the promise is rejected. The then() method returns a new promise, allowing for chaining.

catch(): This method is used to handle errors that occur during the promise's execution. It takes a single argument, which is a function to be called when the promise is rejected. Similar to then(), it also returns a new promise, allowing for chaining.

Here’s a basic example of creating and using a Promise

// Creating a new Promise
const examplePromise = new Promise((resolve, reject) => {
setTimeout(() => {
// After 1 second, the promise is fulfilled with the value "Hello, World!"
resolve("Hello, World!");
}, 1000);
});

// Using the Promise
examplePromise
.then((value) => {
console.log("Promise fulfilled with value:", value);
})
.catch((error) => {
console.error("Promise rejected with reason:", error);
});

In this example, the examplePromise is created with a function that contains two parameters, resolve and reject. The setTimeout function simulates an asynchronous operation, and after 1 second, the promise is fulfilled with the value "Hello, World!". The then() method is used to handle the fulfilled promise, and the catch() method is used to handle any errors.

When To Use Promises

Promises are ideal for handling asynchronous operations in JavaScript. They provide a cleaner, more structured way to manage asynchronous code compared to using callbacks. Here are some situations where using Promises can be beneficial:

AJAX Requests: When making AJAX calls to fetch data from a server, using Promises can help manage the success and error cases more effectively. Many modern libraries, such as Axios, return Promises by default for making HTTP requests.

Database Operations: Database queries often involve asynchronous operations, such as fetching data, updating records, or inserting new entries. Using Promises can help you better manage the success and failure cases, as well as improve code readability.

User Interface Interactions: In some cases, you might want to perform a series of asynchronous actions based on user interactions, such as animations, loading additional content, or making API calls. Promises can help you structure this asynchronous code more effectively.

Third-Party APIs: When interacting with third-party APIs, it’s common to perform asynchronous requests to fetch or update data. Promises can help manage these operations and their success or error cases.

Remember that modern JavaScript also provides the async/await syntax, which is built on top of Promises and further simplifies asynchronous code. However, the underlying concept remains the same, and understanding Promises is essential for working effectively with async/await.

--

--

Patrick Karsh
Patrick Karsh

Written by Patrick Karsh

NYC-based Ruby on Rails and Javascript Engineer leveraging AI to explore Engineering. https://linktr.ee/patrickkarsh

No responses yet