JavaScript | promise resolve() Method

31 May, 2022
The Promise is an object that represents either completion or failure of a user task. A promise in JavaScript can be in three states pending, fulfilled or rejected. The main advantage of using a Promise in JavaScript is that a user can assign callback functions to the promises in case of a rejection or fulfillment of Promise, also by using promises one can easily handle the control flow of all the Asynchronous events or data upcoming. As the name suggests a promise is either kept or broken. So, a promise is either completed(kept) or rejected(broken).Promise resolve() method: Promise.resolve() method in JS returns a Promise object that is resolved with a given value. Any of the three things can happened: 
If the value is a promise then promise is returned.
If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state.
The promise fulfilled with its value will be returned.
Syntax: 
Promise.resolve(value);
Parameters: Value(s) to be resolved by this Promise.Return Value: Either the promise of the promise fulfilled with its value is returned.Examples:  
javascript
<script>    var promise = Promise.resolve(17468); promise.then(function(val) {    console.log(val);});//Output: 17468</script>
Output: 
17468
The above example is made using old version approach we may also use the newest arrow function based approach and also try to avoid writing var datatype since it may mix up several other variables and may produce incorrect result. Below is the shortest approach for the above stated approach.
Following is the code snippet which shows the other version of the above illustrated approach-
Javascript
Promise.resolve(17468).then((value) => console.log(value)); // This code is contributed by Aman Singla....
Output:
17468
Resolving an array:  Here in the below example, we will be using a timer function called setTimeout() will be responsible for the execution of the values which are passed inside resolve() which is passed inside that timer function.
javascript
<script>     const promise = new Promise((resolve, reject) => {        setTimeout(() => {            resolve([89, 45, 323]);        }, 5000);    }); promise.then(values => {    console.log(values[1]);}); </script>
Output: 
45 
Resolving another Promise: In below example we will be resolving the first promise inside another newly created promise in which we have defined one timer function (setTimeout).
javascript
<script>     const promise = Promise.resolve(3126); const promise1 = new Promise((resolve, reject) => {    setTimeout(() => {        promise.then(val => console.log(val));    }, 5000);}); promise1.then(vals => {    console.log(vals);}); </script>
Output: 
3126 
Supported Browsers: 
Google Chrome 32 and above
Mozilla Firefox 29 and above
Opera 19 and above
Safari 8 and above
Edge 12 and above
Internet Explorer not supported
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
sooda367
ysachin2314
amansingla
kumargaurav97520
javascript-functions
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?