AppDividend
Latest Code Tutorials

Javascript Promise Resolve Example | Promise.resolve()

0

Javascript Promise Resolve Example | Promise.resolve() Tutorial is today’s topic.  The Promise.resolve() method returns the Promise object that is 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 returned promise will “follow” that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.

Javascript Promise Resolve Example

The promise is the particular 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’ concept.

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

If the job finished successfully, with the result value.

Promise example

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

Promise.resolve(value);

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

The Promise that 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 Promise that is resolved.

Let us take a simple example. 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 and 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 it 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 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 basically the same as

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

there is a subtlety.

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

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

With this in mind – when the spec was created the promise constructor is throwing safely.

In general, Promise.resolve() is used for casting the objects and foreign promises (thenables) to promises. That’s its use case.

Conclusion

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

If a function returns the promise, what happens is, the next then 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. returns x is equivalent to return Promise.resolve(x)
  2. throws x is equivalent to return Promise.reject(x)  

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

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

3) When the x is the Promise that is pending: return x will return a pending Promise, and it will be evaluated on the subsequent then.

Finally, Promise.resolve() Tutorial is over.

Recommended Posts

Javascript Promise Reject

Javascript Promise All Example

Javascript Promise Race Example

Leave A Reply

Your email address will not be published.

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