JavaScript Promise.resolve: The Complete Guide

The promise is the JavaScript object that links a “producing code” and the “consuming code” together. In regular terms, this is the “subscription list”. It is like the producers’ and consumers’ concepts.

Javascript promise.resolve()

JavaScript Promise.resolve() is a built-in function that returns the Promise object resolved with the given value. If the value is the promise, that promise is returned; if the value is a thenable(i.e., has a “then” method), then the returned promise will “follow” that thenable, adopting its eventual state; otherwise, the returned promise will be fulfilled with the value.

The “producing code” takes whatever time it needs to produce the promised result, and a “promise” makes the result available to all subscribers when it’s ready.

If the job is finished successfully, with the result value.

Promise example

Syntax

The syntax for JavaScript Promise.resolve() is the following.

Promise.resolve(value);

Parameters

This Promise resolves the value parameter. It can also be the Promise or a thenable to resolve.

The promise is resolved with the given value, or the promise passed as the value if the value was a promise object. The static Promise.resolve() function returns the resolved Promise.

Example

Let us take a simple example. First, write the following code inside the app.js file.

// app.js

const p1 = new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
	setTimeout(() => {
		resolve('AppDividend');
	}, 1000);
});

p1.then(values => { 
	console.log(values);
});

Save the file, go to the terminal, and run the file by typing the node app command.

Javascript Promise Resolve Example | Promise.resolve() Tutorial

Here, the p1 promise is thenable and eventually returns the value AppDividend.

Resolving an array

Let us take a simple example that resolves an array.

// app.js

const p1 = new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
	setTimeout(() => {
		resolve([18, 21, 22]);
	}, 1000);
});

p1.then(values => { 
	console.log(values[0]);
});

It will return the 18 value.

Javascript Promise Resolve Example

Resolving another Promise

Let us take the following example.

// app.js

const p = Promise.resolve(21);

const p1 = new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
	setTimeout(() => {
		p.then(value => console.log(value));
	}, 1000);
});

p1.then(values => { 
	console.log(values);
});

Here, we are resolving a promise inside a promise. So the final output will be the value of the first promise.

Promise.resolve() Tutorial

Difference between Promise.resolve() and new Promise()

Syntax of Promise.resolve() is following.

Promise.resolve(x);

that is the same as

new Promise(function(r){ r(x); });

there is a subtlety.

Promise returning functions should generally guarantee that they should not throw synchronously since they might throw asynchronously.

To prevent unexpected results and race conditions – throws are usually converted to returned rejections.

With this in mind, the promise constructor threw safely when the spec was created.

Promise.resolve() is used to cast objects and foreign promises (thenables) to promises. So that’s its use case.

Conclusion

The rule is that if a function is inside, the handler returns the value, and the promise resolves/rejects that value.

If a function returns the promise, the next, then the clause will be the then clause of a promise a function returned.

Making things special, inside a then handler function:

1) When x is the value (number, string, etc.):

  1. It returns x is equivalent to return Promise.resolve(x)
  2. It throws x is equivalent to return Promise.reject(x)  

2) When x is the Promise that is already settled (not pending anymore):

  1. It returns x is equivalent to return Promise.resolve(x) if the Promise was already resolved.
  2. It returns x is equivalent to return Promise.reject(x) if the Promise was already rejected.

3) When the x is the pending Promise, return x will return a pending Promise, which will be evaluated later.

That’s it for this tutorial.

Recommended Posts

Javascript Promise reject()

Javascript Promise all()

Javascript Promise race()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.