Are Promise.resolve and new Promise(resolve) interchangeable

爱⌒轻易说出口 提交于 2019-12-01 06:56:24

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!