Javascript Promises

Javascript 10 min min read Updated: Mar 09, 2026 Intermediate
Javascript Promises
Intermediate Topic 13 of 15

In modern JavaScript, asynchronous operations such as fetching data from servers or reading files can take time to complete. To handle these operations efficiently, JavaScript provides a feature called Promises.

A promise represents a value that may be available now, in the future, or may never be available if an error occurs. Promises provide a structured way to manage asynchronous operations and avoid deeply nested callback functions.

What is a Promise?

A Promise is an object that represents the eventual completion or failure of an asynchronous operation. It allows developers to handle results once the operation finishes.

Key Point: A Promise represents the result of an asynchronous operation that may complete in the future.

States of a Promise

A Promise can exist in one of three states:

  • Pending – The operation is still in progress.
  • Fulfilled – The operation completed successfully.
  • Rejected – The operation failed due to an error.

Creating a Promise

A Promise is created using the Promise constructor. It accepts a function that contains two parameters: resolve and reject.

javascript let promise = new Promise(function(resolve, reject){ let success = true; if(success){ resolve("Operation completed successfully"); } else { reject("Operation failed"); } });
Output

The promise is either resolved with success or rejected with an error.

Handling Promises with then()

The then() method is used to handle successful results of a promise.

javascript promise.then(function(result){ console.log(result); });
Output

Operation completed successfully

Handling Errors with catch()

The catch() method is used to handle errors if the promise is rejected.

javascript promise.catch(function(error){ console.log(error); });
Output

Operation failed

Key Point: then() handles success while catch() handles errors in promises.

Chaining Promises

Promises allow chaining, meaning multiple asynchronous operations can run sequentially.

javascript let promise = new Promise(function(resolve){ resolve(10); }); promise .then(function(result){ return result * 2; }) .then(function(result){ console.log(result); });
Output

20

Advantages of Promises

  • Improves readability of asynchronous code
  • Avoids callback nesting (callback hell)
  • Makes error handling easier
  • Supports chaining of asynchronous operations

Conclusion

Promises provide a powerful and structured way to handle asynchronous operations in JavaScript. By using methods like then() and catch(), developers can manage asynchronous code more effectively.

Promises help make JavaScript applications more organized and easier to maintain.

In the next tutorial, you will learn about Async and Await in JavaScript, which provide an even simpler way to work with promises.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators