---
title: "How to create a new Promise in JavaScript? Give an example."  
description: "How to create a new Promise in JavaScript? Give an example."  
author: "Revati S Misra"  
published: 2023-09-26  
updated: 2023-09-26  
canonical: https://www.mindstick.com/forum/159911/how-to-create-a-new-promise-in-javascript-give-an-example  
category: "javascript"  
tags: ["javascript", "promise"]  
reading_time: 2 minutes  

---

# How to create a new Promise in JavaScript? Give an example.

How to create a new [Promise](https://www.mindstick.com/forum/158328/what-is-a-promise-in-javascript-and-how-do-you-use-it) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)? Give an example.

## Replies

### Reply by Aryan Kumar

Creating a new Promise in JavaScript involves using the **Promise** constructor. Here's an example of how to create a new Promise:

```plaintext
const myPromise = new Promise((resolve, reject) => {
  // Inside this function, you define the asynchronous operation
  // For example, you can simulate an async operation with setTimeout

  setTimeout(() => {
    const success = true; // Replace with your actual logic
    if (success) {
      resolve("Operation succeeded!"); // Resolve the promise with a value
    } else {
      reject("Operation failed!"); // Reject the promise with an error
    }
  }, 2000); // Simulating a delay of 2 seconds
});

// Using the promise
myPromise
  .then((result) => {
    console.log("Success:", result);
  })
  .catch((error) => {
    console.error("Error:", error);
  });
```

In this example, we create a new Promise called **myPromise**. It takes a function as its argument, which receives two parameters: **resolve** and **reject**. Inside this function, you define your asynchronous operation, which in this case is simulated using **setTimeout**.

If the operation is successful, you call **resolve** and pass the result to it. If it encounters an error, you call **reject** and provide an error message or an error object.

You can then use the **.then()** method to handle the successful outcome when the Promise is resolved and the **.catch()** method to handle errors when the Promise is rejected.


---

Original Source: https://www.mindstick.com/forum/159911/how-to-create-a-new-promise-in-javascript-give-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
