kraftwer1
kraftwer1

Reputation: 5731

When should I call Promise.resolve() directly?

I've seen that the native ES6 Promise.resolve() can be invoked directly - as a static method. Facebook is using it that way in their Flux examples.

But in what case should I do that? To queue something? Or instead of using window.setTimeout()?

Upvotes: 11

Views: 3888

Answers (3)

jib
jib

Reputation: 42450

Semantically, functions that return promises are asynchronous functions, and, as you know, can be chained together:

a().then(b).then(c).catch(failure);

While synchronous functions can be part of asynchronous chains as well, this only works because the .then function automatically promotes return values from the functions you pass it, to promises. E.g. If b and/or c return values that are not promises, then the chain still works, but if a returns a non-promise value then that's TypeError.

In most cases you probably know what areturns, so this is fine, but in cases where you don't know what a will return (say you're doing generic programming), then you can do this:

Promise.resolve(a()).then(b).then(c).catch(failure);

a, b and c are now treated the same in this regard:

  • If a returns 1, then b will be called soon with 1.
  • If a returns a promise, then b will be chained behind a.

The method Promise.reject accomplishes the same thing for failure chains.

Additonally, these methods can be handy in cases where you just need a promise that resolves right away. E.g.

[a, b, c].reduce((p, f) => p.then(f), Promise.resolve()).catch(failure);

Upvotes: 3

Felix Kling
Felix Kling

Reputation: 816462

Promise.resolve(42) is just a shorthand for

new Promise(function(resolve) {
    resolve(42);
});

So whenever you find yourself doing something like this, i.e. creating a promise from an existing value, you can use Promise.resolve instead.

Upvotes: 3

Sergey Rybalkin
Sergey Rybalkin

Reputation: 3026

You should call Promise.resolve(object) when you need to create a promise that is already resolved. For example you may have a function that starts downloading a resource from the server or returns its cached version:

function getImage(imageUrl) {
    if (imageUrl in this.cache) {
        // no need to download, return immediately
        return Promise.resolve(this.cache[imageUrl]);
    } else {
        return new Promise(function(resolve, reject) {
            // start downloading and eventually resolve
        });
    }
}

Upvotes: 20

Related Questions