JavaScript promise resolve() Method
Last Updated :
09 Jan, 2025
The Promise.resolve() method in JavaScript returns a Promise object that is resolved with a given value. If the value is a promise, it returns that promise; otherwise, it resolves the value as a new promise, making it useful for simplifying asynchronous code handling.
What is Promise resolve() method
The promise.resolve() method in JS returns a Promise object that is resolved with a given value. Any of the three things can happen:
- If the value is a promise then the promise is returned.
- If the value has a “then” attached to the promise, then the returned promise will follow that “then” to 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.
Example 1: In this example, we creates a resolved promise with the value 17468. The .then() method handles the resolved value, logging 17468 to the console as the output.
javascript
let promise = Promise.resolve(17468);
promise.then(function (val) {
console.log(val);
});
//Output: 17468
Example 2: Following is the code snippet that shows the other version of the above-illustrated approach-
JavaScript
Promise.resolve(17468).then((value) => console.log(value));
Example 3: In this 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
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve([89, 45, 323]);
}, 5000);
});
promise.then(values => {
console.log(values[1]);
});
Output:
45
Example 4: In this example, we will be resolving the first promise inside another newly created promise in which we have defined one timer function (setTimeout).
javascript
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);
});
Output:
3126
We have a complete list of Javascript Promise methods, to check those please go through the Javascript Promise Complete Reference article.
Supported Browsers:
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.
JavaScript promise resolve() Method – FAQs
Can Promise.resolve() handle errors?
No, Promise.resolve() only creates a resolved promise. To handle errors, you would use Promise.reject() or chain .catch() to a promise.
Can Promise.resolve() take another promise as an argument?
Yes, if Promise.resolve() is passed another promise, it will return that same promise without creating a new one.
How do you use Promise.resolve() with synchronous values?
Promise.resolve(value) can wrap a synchronous value in a promise, allowing you to handle it as a promise, useful for consistent promise-based logic.
What happens when Promise.resolve() is passed null or undefined?
When passed null or undefined, Promise.resolve() returns a promise resolved with that value.
Can Promise.resolve() be used to simplify async functions?
Yes, Promise.resolve() can help convert non-promise values into promises, simplifying async function logic by ensuring all results are handled as promises.