Promises rules
no-async-constructor
Section titled “no-async-constructor”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.
What to do
Section titled “What to do”Move the asynchronous work into an async factory function or an init method that callers await.
Example
Section titled “Example”constructor() { this.load(); } reports async when load returns a promise.
no-try-promise
Section titled “no-try-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.
What to do
Section titled “What to do”Await the promise inside the try, or handle its rejection with .catch and remove the try.
Example
Section titled “Example”try { save(record); } catch (error) { report(error); } reports open when save returns a promise.
prefer-promise-shorthand
Section titled “prefer-promise-shorthand”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).
What to do
Section titled “What to do”Replace the constructor with Promise.resolve(value) or Promise.reject(error).
Example
Section titled “Example”new Promise((resolve) => resolve(value)) reports shorthand. Write Promise.resolve(value).