What is a promise in JavaScript?17 Mar 2025 | 3 min read The Promise in JavaScript may look quite complicated to understand at first sight, but in reality, it is quite simple and is not rocket science. In JavaScript, a promise is just like a promise that you make in real life to show that you are committed to doing something. For example, I promise to get good marks in mathematics, and then this Promise has two outcomes, either it will be fulfilled (or resolved) or not fulfilled (or be rejected). So if I get good marks, the Promise is resolved, but if I fail to get good marks, it will not be resolved because I could not keep my Promise. However, in JavaScript, a promise has three outcomes; Promise gets resolved, gets rejected, or pending state, which means the Promise is not completed yet but may get completed after some time, so it is not rejected till now and is in the pending state. ![]() Let's see another example to understand the concept of Promise in JavaScript: Suppose, we have made a request to fetch some data from the server by using the Promise if we get that data from the server successfully, the Promise will be considered as resolved successfully (or completed), but in case, we don't get that data from the server due to any reason, that means the Promise was rejected or not completed. Syntax of Promise in JavaScript Let's see how to create and use a promise in JavaScript? First of all, we have to create a promise using the constructor: The Promise has two parameters as we already have seen the syntax of the Promise: Here is the last part that is the condition. If the condition is satisfied, the Promise will be resolved; otherwise, Promise will be rejected. So here is our first Promise. Now let's use it. The then() method As we already discussed above, for a promise, there are two main cases: one for the resolved and another for the rejection. When the Promise gets resolved, something will happen next that depends on what we want to do with the resolved Promise. This ".then();" method is only called when the Promise is resolved( completed successfully), and what we passed in it will be displayed. For example, we can pass a message in it for the user. The .catch(); method This method is called when the promise is rejected (or failed). And we can also pass a message in it for the user. We can write the catch method just after the "then ()" method. So if the Promise gets resolved, then we will get the message passed in the ".then()" method on the screen, but if the Promise gets rejected then, we will get the message we passed in the ".catch()" method. This means in both cases; we will get different messages on the screen. We can understand how promise works more easily with the help of the following example: Program Explanation of the program In the above program, we have created a promise using the constructor and passed two parameters resolve, reject. Inside the Promise definition, we also have created a variable "condition" and assigned the value 9 to it. In addition, using the (if) statement, we have checked the value of the variable. If the condition gets satisfied, the "if" part will execute and promise get resolved (the then() method will be called too), if the condition does not get satisfied, the else part will be executed, and Promise gets rejected(then catch() will be executed). Output To see the output in the console, press the "f12" key to open the inspect mode in the web browser. ![]() Note: You can modify the program code if you want to execute the else part of the Promise by changing the condition to true or false.Next TopicWhat is hoisting in JavaScript |
You must have noticed the efficiency of the web application's functionalities or how web applications handle user events. or JavaScript Throttle concept provides the mechanism to improve the performance and speed of the application. The throttling mechanism is also known as throttle method is a...
8 min read
JavaScript offers in-built methods to open and close the browser window to perform additional operations like robot window etc. These methods help to open or close the browser window pop-ups. Following are the window methods: open() close() The window.open method is used to open a new web page...
6 min read
In this section, we will learn about the implementation of Stack using JavaScript arrays. What is a Stack A data structure in which we place the elements in Last In First Out, i.e., LIFO principle. The LIFO principle means the arrangement of the element in such a...
6 min read
Concept Design patterns can be termed as well-documented solutions to the most commonly occurring problems in software engineering. It turns quite hectic for developers to bang out their heads on the problem that someone else has already solved. Every developer aspires to write industrial-level code that...
9 min read
The javascript provided keyboard events for the selection, type, and use of particular forms of information. The event occurs when the key presses the key up and the key down as per action. We can use the html tag, javascript function, or addEventListener event. It...
9 min read
The Factory Function is related to the constructor function or class functions. However, it produces and returns an object without requiring a new one. Factory Functions in JavaScript are identical to constructorfunctions. However, they don't require the 'this' keyword for inner values or the 'new' keyword...
3 min read
JavaScript is a potent scripting language that gives programmers the ability to make dynamic, interactive web sites. Finding a specific element's parent element is a frequent operation when working with the Document Object Model (DOM). Here, we'll look at a variety of JavaScript-based approaches to accomplishing...
6 min read
. In this section, we are going to learn about whether the array is null or empty, or undefined. We will use JavaScript to do this. Sometimes the unexpected output or software crashes is occurred by the empty or null array. If we want to avoid...
3 min read
We will understand how to manage <select> option in JavaScript in this tutorial. HTML Select Option A <select> option facilitate us the options list. It permits us to choose a single or more than one option. We use the <option> and <select> elements for forming a <select>...
4 min read
Sometimes, programmers require to create a string which is generated by selecting the random characters. Random String Generator helps to create a random string by choosing some characters randomly. This string can be a simple character string or an alpha-numeric string. In this chapter, you will...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India