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 till the final state.
- The promise fulfilled with its value will be returned.
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 will see a basic example of a promise. resolve method.
The above example is made using the 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 results. Below is the shortest approach for the above-stated approach.
Example: Following is the code snippet that shows the other version of the above-illustrated approach-
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.
Resolving another Promise: In the below example, we will be resolving the first promise inside another newly created promise in which we have defined one timer function (setTimeout).
- 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