skellertor
skellertor

Reputation: 1115

Difference in timing when returning a resolved Promise

I have a function that does a bunch of work then returns a resolved Promise. Is there a difference in the timing when each will be resolved?

Option 1

function returnPromise() {
  // do a bunch of stuff
  return Promise.resolve();
}

and this way:

Option 2

function returnPromise() {
  return new Promise((resolve, reject) => {
    //do a bunch of stuff
    resolve();
  });
}

Does Option 1 release control after all the work is done, and Option 2 release control as soon as the function is called? I feel like this has root in the fact I don't fully understand the event loop. Thanks in advance for helping me understand this.

Upvotes: 1

Views: 40

Answers (2)

jfriend00
jfriend00

Reputation: 707846

Is there a difference in the timing when each will be resolved?

No difference. The Promise executor (the callback you pass to new Promise()) is called immediately and synchronously. So, in BOTH cases, you are immediately and synchronously returning an already resolved promise and all the code in your function has already executed.

There should be no meaningful difference in execution timing. Of course, one might take slightly different number of CPU cycles to execute, but both have the exact same outcome and both immediately return a promise that is already resolved so there should be no difference to the calling code at all.

Promise.resolve() is just meant as a more compact (and probably more efficient) means of creating an already resolved promise.

Does Option 1 release control after all the work is done, and Option 2 release control as soon as the function is called?

They both return when all your work is done and they return an already resolved promise. This is because the executor callback to new Promise() is called synchronously (before your function returns).

I feel like this has root in the fact I don't fully understand the event loop. Thanks in advance for helping me understand this.

In this particular circumstance, all your code is run synchronously so there is no particular involvement of the event loop. The returnPromise() function returns (in both cases) when your code in the function is done executing synchronously. There is no async code here.


The event loop gets involved when you do .then() on the returned promise because even though the promise (in both cases) is already resolved, the .then() handler gets queued in the event queue and does not run until the rest of your Javascript after your .then() handler is done executing.

console.log("1");
returnPromise().then(function() {
    console.log("2");
});
console.log("3");

This will generate identical output results:

1
3
2

with both versions of your returnPromise() function because .then() handlers are queued until the next tick of the event loop (e.g. after the rest of your current Javascript thread of execution is done).

Upvotes: 2

nicholaswmin
nicholaswmin

Reputation: 22979

Is there a difference with doing it this way?

No, both your examples do the exact same thing.

Promise.resolve and Promise.reject are simply static methods on the Promise Class that help you avoid constructing a whole Promise when you don't really need to.

Upvotes: 2

Related Questions