Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Create a Resolved Promise in JavaScript with Promise.resolve()

    Marius SchulzMarius Schulz
    javascriptJavaScript

    The Promise.resolve() method returns a Promise object that is resolved with the given value. It can be used to convert “promise-like” objects to native Promise objects: If you pass a thenable (an object with a then() method) to Promise.resolve(), the returned Promise object will eventually adopt the same state.

    Note that Promise.resolve() doesn't necessarily return a fulfilled promise. If it receives a thenable that ends up being rejected, the returned Promise object will be rejected as well. Check out States and Fates for a more detailed explanation.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 Let's take a look at the Promise.resolve method, which can do a couple of things. We can call Promise.resolve and pass it any value. What we'll get back is a promise that has been immediately fulfilled with that value.

    00:15 Let me go ahead and store this promise in a variable. If I now pass it to Promise.resolve, we see that we get back the exact same promise. We can see that the references are identical. Keep in mind that whenever you pass a native promise object to the Promise.resolve method, you get back the exact same object.

    00:36 Let's now take a look at rejected promises. I'm going to create a rejected promise using the Promise.reject method. I'm going to pass it some error. The JavaScript engine complains that we have an unhandled promise rejection.

    00:52 This is because we haven't attached any rejection handlers to our promise up here. For sake of demonstration, we can safely ignore this error message. In your applications, you want to make sure to attach rejection handlers to all your promises.

    01:05 If we now call Promise.resolve and pass it the rejected promise, we'll see that again we'll get back the exact same reference. It's important to keep in mind that Promise.resolve will not always return a fulfilled promise. If we pass a promise or a promise-like object that is rejected, we'll get back a rejected promise rather than a fulfilled one.

    01:29 I also want to point out that if you call Promise.resolve and pass it an error, you will get back a fulfilled promise. It's a promise that has been fulfilled with that error. Perhaps counterintuitively, this is not a rejected promise. This is because there's no special treatment for errors that we pass to Promise.resolve.

    01:51 Let's now take a look at the most interesting thing that Promise.resolve can do for us. That is converting promise-like objects to proper native promises. Let's assume that instead of the fetch API, we're using good old jQuery to make the Ajax request. In this case, we won't need the manual response parsing.

    02:12 If we're now going to run this code, we're going to see that it doesn't work. jQuery was written long before we had native promises in JavaScript. jQuery ships with its own promise implementation. That implementation doesn't have a method called "finally."

    02:27 We can fix this by using Promise.resolve. This way, the non-standard promise returned by the getJSON method will be converted to a proper native promise. If we refresh now, we see that our page is working again. This works for all promise-like objects with a then method. These objects are also known as thenables.

    02:49 To summarize, you can use the Promise.resolve method to take any non-standard promise and convert it into a proper native promise. This way, you have access to all the standardized promise methods. You also get better interoperability with any library expecting a native promise.

    Discuss

    Discuss