Are Promise.resolve and new promises (allow) interchangeably

I think that Promise.resolveand new Promise(resolve)interchangeably.

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);
});

IN.

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.

Therefore, I think that if I do not need to β€œreject” anything. I just can 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 to use RSVP.resolve();for replacement? 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);
});

Prints something else instead of "HI". So you can use RSVP.resolve (); Here? Are these two interchangeable?

+4
source share
1 answer

First of all

I think Promise.resolve and the new promise (resolution) are interchangeable.

. Promise.resolve , , new Promise(resolve) , .


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

, setTimeout, . , then setTimeout. .


, . Promise.resolve. , , - .

+3

Source: https://habr.com/ru/post/1618078/


All Articles