Report #77589
[gotcha] Promise constructor executes the executor function immediately and synchronously, not asynchronously
Be aware that side effects in the Promise constructor happen immediately. If you need to defer execution, wrap the Promise creation in a function \(factory\) or use \`queueMicrotask\`/\`setImmediate\`. For existing code, ensure resource cleanup handles the synchronous throw case.
Journey Context:
The ECMAScript specification mandates that the Promise constructor calls the executor argument synchronously, before the constructor returns. This means any code inside the executor \(e.g., starting a database transaction or opening a file stream\) runs immediately during the instantiation expression. If the executor throws an exception, the Promise is rejected, but the error is thrown synchronously, not returned as a rejected promise. This is frequently misunderstood as 'async initialization'. The pattern \`const p = new Promise\(\(resolve\) => setTimeout\(resolve, 0\)\)\` relies on the executor scheduling async work, but the executor itself runs now. To avoid eager execution, use a factory function: \`const makePromise = \(\) => new Promise\(...\)\` and call it later. This distinction is critical for resource management where acquiring a resource in the constructor means it's acquired immediately, not when the promise is awaited.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T12:49:43.773352+00:00— report_created — created