no-async-constructor
Disallows async work inside constructors.
Rule details Problem
Section titled “Rule details ”ProblemConstructors return before async work finishes. This rule reports patterns that leave an instance half-initialized or drop promise errors on the floor.
It reports:
awaitinside a constructor- promise chains like
.then(),.catch(), and.finally() - async IIFEs called from the constructor
- bare calls to async methods on
this, such asthis.load() - promises from async methods stored in a local variable and never used
It does not report this.loadPromise = this.load();, because the promise stays on the instance and can be consumed by
callers.
Examples
Section titled “Examples” Incorrect
class Database { constructor() { this.connect().then(() => logReady()); }
async connect() {}} Correct
class Database { private constructor() {}
static async create() { const database = new Database(); await database.connect(); return database; }
async connect() {}}Common refactors
Section titled “Common refactors”- use
static async create()when construction depends on async setup - expose an
init()method and make callers await it before use - if you must start work in the constructor, store the promise on
thisso the lifecycle is visible
Related rules
Section titled “Related rules” require-module-level-instantiation Keeps singleton-style setup at module scope