Search code examples
javascriptes6-promise

Difference between Promise.resolve and empty Promise Object


What is the difference between using Promise below ways.

Way 1:

function xyz() {
    return Promise.resolve();
}

Way 2.

function xyz() {
    return new Promise(() => {});
}

So, in a project i was been following they switched the return value of function xyz from way 1 to way 2. I am not able to understand why did they switch from way 1 to way 2.


Solution

  • In Way 1, you are returning a promise whose state is fulfilled.

    In Way 2, you are returning a promise whose state is pending and it will be pending forever because nobody retained either the resolve() or reject() function references which are the only way to change its state.

    I know of no reason to ever do what you have in Way 2 as it's just not useful to have a promise that can never resolve or reject. These two examples are certainly not the same result.