Are Promise.resolve and new Promise(resolve) interchangeable
Asked Answered
C

1

5

I think Promise.resolve and new Promise(resolve) are interchangeable.

Consider this:

A.

new RSVP.Promise(function (resolve, reject) {
    resolve();
}).then(function () {
    return new RSVP.Promise(function (resolve) {
        resolve("HI")
    });
}).then(function (result) {
    console.log(result);
});

B.

new RSVP.Promise(function (resolve, reject) {
    resolve();
}).then(function () {
    return RSVP.resolve("HI");
}).then(function (result) {
    console.log(result);
});

Both print "HI" as I expected.

So I think if I don't need to "reject" anything. I can just write RSVP.resolve(); for simplicity.

But consider this example:

new RSVP.Promise(function (resolve, reject) {
    resolve();
}).then(function () {
    return new RSVP.Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve("HI")
        }, 3000);
    });
}).then(function (result) {
    console.log(result);
});

How can I use RSVP.resolve(); to replace? I tried for example:

new RSVP.Promise(function (resolve, reject) {
    resolve();
}).then(function () {
    return setTimeout(function () {
        return new RSVP.resolve("HI");
    }, 3000);
}).then(function (result) {
    console.log(result);
});

This prints something else instead of "HI". So is it possible to use RSVP.resolve(); here? Are these two interchangeable?

Countermand answered 1/12, 2015 at 6:59 Comment(1)
You've got the same issue as in Promise constructor with reject call vs throwing errorZollie
A
8

First and foremost

I think Promise.resolve and new Promise(resolve) are interchangeable.

Nope. Promise.resolve will create a promise which is already resolved, whereas new Promise(resolve) creates a promise which is neither resolved nor rejected.


In the last example,

return setTimeout(function () {
    return new RSVP.resolve("HI");
}, 3000);

means that, you are returning the result of setTimeout function, not a promise object. So, the current then handler will return a resolved promise with the result of setTimeout. That is why you are seeing a weird object.


In your particular case, you want to introduce a delay before resolving the promise. It is not possible with Promise.resolve. The penultimate method you have shown in the question, is the way to go.

Anh answered 1/12, 2015 at 7:10 Comment(2)
could you tell me when to use RSVP.resolve() is preferred?Countermand
@Countermand Whenever you need to create a promise which is already resolved. For example, let's say you are writing a function which queries the internet for the given word. Since the querying is an asynchronous task, you return a Promise from it. But you don't want to do the querying everytime, as it is very slow. So, you cache the result of the query in a JavaScript object. Next time when the request comes for the same keyword, you need to return a promise but with the values, not doing an async operation. In that case, you can simply do if (k in cache) return Promise.resolve(cache[k])Anh

© 2022 - 2024 — McMap. All rights reserved.