Skip to content

No Async Constructor

Error
small-rules/no-async-constructor

Disallow asynchronous operations inside class constructors. Constructors return immediately, so async work causes race conditions, unhandled rejections, and incomplete object states.

Diagnostic Messages

asyncIifeInConstructor
Refactor this asynchronous operation outside of the constructor. Async IIFEs create unhandled promises and incomplete object state.
awaitInConstructor
Refactor this asynchronous operation outside of the constructor. Using 'await' in a constructor causes the class to be instantiated before the async operation completes.
orphanedPromise
Refactor this asynchronous operation outside of the constructor. Promise assigned to '{{variableName}}' is never consumed - errors will be silently swallowed.
promiseChainInConstructor
Refactor this asynchronous operation outside of the constructor. Promise chains (.then/.catch/.finally) in constructors lead to race conditions.
unhandledAsyncCall
Refactor this asynchronous operation outside of the constructor. Calling async method '{{methodName}}' without handling its result creates uncontrolled async behavior.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-async-constructor": "error"
}
}

Examples

Promise chain inside constructor
class BadThen {
constructor() {
fetch('/api').then(r => { this.data = r; });
}
}
Static async factory method
class GoodFactory {
private constructor(private data: string) {}
static async create(): Promise<GoodFactory> {
const data = await fetchData();
return new GoodFactory(data);
}
}