Skip to content

Promises rules

Reports a statement of a class constructor that calls something that returns a promise.

On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.

A constructor cannot await, so the object is returned before the work finishes, and a rejection goes unhandled.

Move the asynchronous work into an async factory function or an init method that callers await.

constructor() { this.load(); } reports async when load returns a promise.

Reports a try statement whose body only starts promises that it does not await, because the catch clause never sees their rejections.

On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.

A catch clause sees only errors thrown while the try body runs, not a promise that rejects later.

Await the promise inside the try, or handle its rejection with .catch and remove the try.

try { save(record); } catch (error) { report(error); } reports open when save returns a promise.

Reports new Promise whose executor only calls its resolve or reject parameter with one argument.

On in the all config.

A promise that settles at once reads more clearly as Promise.resolve(value) or Promise.reject(error).

Replace the constructor with Promise.resolve(value) or Promise.reject(error).

new Promise((resolve) => resolve(value)) reports shorthand. Write Promise.resolve(value).