When is the body of a Promise constructor callback executed?

Immediately, yes, by specification.

From the MDN:

The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object)

This is defined in the ECMAScript specification (of course, it’s harder to read…) here (Step 9 as of this edit, showing that the executor is called synchronously):

  1. Let completion be Completion(Call(executor, undefined, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »)).

(my emphasis)

This guarantee may be important, for example when you’re preparing several promises you then pass to all or race, or when your executors have synchronous side effects.

Leave a Comment