0

Is there a spec / implementation detail on how "eager" is the evaluation of the callbacks of a promise? I mean, let's say that I have

var promise = new Promise((resolve) => {
   resolve()
});

console.log(promise); // there is no public field '<state>', but you can see it in the console of the dev tools

I see that promise is already fulfilled; I was expecting that the Promise internal implementation would call the resolve callback on an following time, leaving a "time window" with promise still unfulfilled.

Is this "eager" evaluation of the resolve callback "by design"? Is this just an implementation detail?

Vito De Tullio
  • 2,332
  • 3
  • 33
  • 52
  • is it resolved though? - `promise` in that code is forever pending, as it is never fulfilled nor rejected – Bravo Jun 16 '22 at 08:41
  • by the way, the function you pass to new Promise is run immediately - the promise returned can be fulfilled or rejected in that callback by calling the passed in functions (the first fulfills and the second rejects - the convention, for some reason, is `function(resolve, reject) {}`) - however, yor "resolve" function takes no arguments, so can't possibly effect the state of the Promise, hence it will be forever pending – Bravo Jun 16 '22 at 08:54
  • Turned your code into a snippet and added a `then` call. This demonstrates that the promise is **not** resolved. – trincot Jun 16 '22 at 08:59
  • That's a bit misleading @trincot - a Promise is either `pending`, `fulfilled` or `rejected` :p - my pet hate with Promises is that the "onFulfilled" callback is ALWAYS named "resolve" in almost every codebase ... I'm a rebel, I call it `fulfil` :p – Bravo Jun 16 '22 at 09:01
  • @Bravo, resolving has a meaning of its own right. A promise can be resolved without being yet defined whether it will fulfil or reject. This happens when it is "locked in". [See this answer](https://stackoverflow.com/questions/35398365/js-promises-fulfill-vs-resolve/56850392#56850392) I have on this terminology. So calling the callback `fulfil` is not really covering its use better. – trincot Jun 16 '22 at 09:18
  • my example was wrong, I want to understand if the resolve callback of the executor is executed eagerly or not – Vito De Tullio Jun 16 '22 at 09:44
  • @trincot - very good point - hadn't thought of that! – Bravo Jun 16 '22 at 09:44
  • @VitoDeTullio - it is called immediately, not eagerly – Bravo Jun 16 '22 at 09:44

1 Answers1

3

The callback passed to the Promise constructor is executed synchronously by that constructor. So when the new operator returns the constructed object, the callback has already run.

This is by the ECMA Script specification. The procedure for new Promise(excecutor) describes in step 9 that the executor is called and in step 11 that the constructed promise object is returned.

Mozilla Contributors write:

Syntax

new Promise(executor)

Parameters

executor

A function to be executed by the constructor, during the process of constructing the new Promise object.

The constructor callback function is typically used to initiate an asynchronous process (by relying on an asynchronous API, like setTimeout), which can resolve the promise at a time that the constructor callback has already returned.

function executor(resolve) { 
    setTimeout(resolve, 100);
}

var promise = new Promise(executor);

In your example, that callback does not rely an such asynchronous API, but synchronously calls the resolve callback. That means that when the constructed promise object is assigned to your promise variable, that promise is already in a fulfilled state.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • which beggers the question, why does OP state that the promise is fulfilled! – Bravo Jun 16 '22 at 08:56
  • @Bravo It isn't. Probably they meant that their `resolve` function (i.e. executor) was called... – FZs Jun 16 '22 at 09:01
  • ahh, @FZs - how could they tell that? it's an empty function :p – Bravo Jun 16 '22 at 09:05
  • no, I mean that the promise is fulfilled. as in: I open the debug tools, evaluate new Promise( resolve => resolve()); and I see `{ : "fulfilled", : undefined }` – Vito De Tullio Jun 16 '22 at 09:35
  • Vito, I see you have updated your question's code after I posted my answer, so I have updated my answer accordingly. – trincot Jun 16 '22 at 10:30