Are Promise.resolve and new Promise(resolve) interchangeable

前端 未结 1 778
鱼传尺愫
鱼传尺愫 2021-01-06 04:27

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

Consider this:

A.

new RSVP.Promise(function (         


        
相关标签:
1条回答
  • 2021-01-06 05:14

    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.

    0 讨论(0)
提交回复
热议问题