Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | promise resolve() Method

  • Last Updated : 13 Sep, 2021

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. 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: 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Promise.resolve(value);

Parameters: 
Value 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

Resolving an array:  

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: 

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 6.0 and above
  • Internet Explorer 9.0 and above
  • Mozilla 4.0 and above
  • Opera 11.1 and above
  • Safari 5.0 and above

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :