always-return
return
or throw
statement inside then()
or catch()
callbacks. This prevents cases where a promise might resolve with undefined
unexpectedly, leading to difficult-to-debug issues in asynchronous code flows.return
or throw
in promise handlers, the rule can help prevent subtle bugs that could arise from implicit returns in arrow functions or overlooked return statements. For example, a developer might unintentionally omit a return statement, causing the promise to resolve with undefined
, which can lead to runtime errors when the subsequent code expects a value.ignoreLastCallback
option, allows teams to tailor the rule to their specific project needs or coding styles, providing flexibility. For instance, a team could choose to ignore this rule for the last callback in a promise chain if their application logic or style guide deems it reasonable, ensuring the rule adds value without being overly prescriptive.avoid-new
bad
to fix
code snippets where checkData
function is refactored to utilize asyncValidateData
instead of creating a new promise.asyncValidateData
, developers are nudged towards leveraging abstractions that encapsulate the asynchronous logic.checkData
increases the risk of mishandling rejection or resolution states, which is mitigated when reusing asyncValidateData
.asyncValidateData
) instead of scattering new Promise instantiations across the codebase. This facilitates easier updates and optimizations to the asynchronous logic, as changes need to be made in fewer places.catch-or-return
.catch()
handler or returned for error handling further up the call chain, promoting robust error handling practices in asynchronous code, which is crucial to avoid unhandled promise rejections that can lead to runtime errors and application instability.allowThen
for error handling in a second .then()
method parameter, allowFinally
for using .finally()
as a termination method, and specifying alternative termination methods. This accommodates various coding styles and requirements while still enforcing error management.no-callback-in-promise
no-multiple-resolved
no-native
Promise
constructor when there could be simpler or more advanced abstractions available which could lead to clearer, more maintainable code.async/await
that can make asynchronous code easier to read and debug, especially in complex applications where understanding the flow of asynchronous operations is vital.Promise
states (pending, fulfilled, rejected) by encouraging patterns that might aid in avoiding common pitfalls such as unhandled promise rejections.no-nesting
no-new-statics
The rule specifically targets the incorrect use of new
with Promise
static methods such as Promise.resolve()
, Promise.reject()
, Promise.all()
, and Promise.race()
. These static methods are meant to be called directly on the Promise
constructor without creating a new instance.
It helps in preventing runtime errors that can occur from incorrectly instantiating Promise
static methods with new
. Since these methods are not constructor functions and are not meant to be called with new
, doing so can lead to unexpected behavior or errors in the code.
It enhances code readability and maintainability by ensuring that Promise
static methods are used correctly. Using new
with these methods is a common mistake among developers, especially those new to JavaScript’s Promise
API, and enforcing this rule can help catch such mistakes early.
The rule provides an automatic fix to incorrect usage by removing the new
keyword when applied to Promise
static methods. This not only helps in correcting the errors but also educates developers about the proper usage of Promise
static methods, promoting best practices within the codebase.
no-promise-in-callback
The rule helps maintain asynchronous code consistency by discouraging the mixing of callback patterns with promises within the same function, which prevents potential confusion regarding how error handling is managed and how the flow of asynchronous operations is understood. Mixing patterns can lead to harder-to-follow code and possible errors in handling asynchronous operations.
It encourages the use of modern JavaScript async/await syntax or thenable chains at a higher level rather than nesting promises inside callbacks. This leads to cleaner, more readable code, which is easier to debug and maintain.
Enforcing this rule helps in identifying and refactoring legacy code that uses mixed asynchronous patterns, promoting the adoption of newer, more efficient patterns of handling asynchronous operations. Such refactoring can lead to better performance and more consistent error handling.
It assists in avoiding potential issues related to the misuse of promises within callbacks, such as unhandled promise rejections or unexpected behavior due to the mixing of different asynchronous control flows, which can be hard to debug and lead to runtime errors.
no-return-in-finally
This rule ensures that the fulfillment or rejection of Promises is consistent by preventing a return
statement in a .finally()
block, which does not affect the promise’s outcome. This ensures developers do not mistakenly attempt to alter the promise result in a finally
block, which is not possible, leading to clearer and more predictable code behavior.
By disallowing return statements in finally
, it avoids confusion regarding the flow of promise resolution and rejection. Since finally
is used for cleanup after operations have completed, regardless of the outcome, injecting a return statement might misleadingly imply control over the promise’s resolution value or state which is not the case.
This rule emphasizes the separation of concerns by ensuring finally
blocks are used solely for cleanup purposes and not for returning values or affecting the promise chain. It promotes writing cleaner and more maintainable asynchronous JavaScript code by keeping the code responsible for resolving or rejecting promises separate from the code intended for cleanup activities.
Enforcing no return in finally
helps in debugging and code readability. When promises do not behave as expected, allowing return statements in finally
could lead to misleading interpretations of the code’s intention. By enforcing this rule, it becomes easier to track down where values are being resolved or rejected in the promise chain, reducing potential debugging time and improving maintainability.
no-return-wrap
Promise.resolve
or Promise.reject
within a Promise
chain, which can lead to code that is more complex and harder to read than necessary.Promise.resolve
or Promise.reject
creates an additional unnecessary promise that must be resolved or rejected, potentially impacting performance.param-names
prefer-await-to-callbacks
await/async
over traditional callback patterns, which can lead to clearer and more maintainable code structures.await/async
, it implicitly supports the handling of promises, which aligns with modern JavaScript practices and facilitates error handling using try/catch
blocks.prefer-await-to-then
This ESLint rule encourages the use of async/await
over then()/catch()/finally()
with promises, which can make code easier to read and maintain by reducing the complexity associated with promise chaining. The use of async/await
often leads to cleaner and more straightforward code that resembles synchronous code structures, improving readability.
The rule includes a check to ensure that it only reports .then()
, .catch()
, or .finally()
usage when not within an async
function or a generator function (yield
). This specificity helps prevent false positives in contexts where await
cannot be used directly, such as at the top-level scope in environments where top-level await
is not allowed.
By including a condition to ignore promises within yield
or await
expressions, the rule smartly targets only those promise chains that can and should be converted to async/await
. This focuses the developer’s attention on refactoring promise usages that will most benefit from async/await
syntax, without distracting with irrelevant recommendations.
The rule provides a clear message, “Prefer await to then()/catch()/finally().”, when a violation is detected. This direct feedback educates developers on modern JavaScript best practices, encouraging them to adopt async/await
in their code. Such specific guidance helps in codebase modernization efforts and aids in maintaining a uniform coding style across a project.
valid-params
Ensures the proper number of arguments are passed to Promise functions
resolve
, reject
, then
, etc., thereby promoting standard-compliant code which leads to more predictable behavior across different JavaScript environments.resolve
or reject
functions, which might lead to confusion regarding the outcome of the Promise or the parameters passed to the subsequent .then()
or .catch()
handlers.always-return
return
or throw
statement inside then()
or catch()
callbacks. This prevents cases where a promise might resolve with undefined
unexpectedly, leading to difficult-to-debug issues in asynchronous code flows.return
or throw
in promise handlers, the rule can help prevent subtle bugs that could arise from implicit returns in arrow functions or overlooked return statements. For example, a developer might unintentionally omit a return statement, causing the promise to resolve with undefined
, which can lead to runtime errors when the subsequent code expects a value.ignoreLastCallback
option, allows teams to tailor the rule to their specific project needs or coding styles, providing flexibility. For instance, a team could choose to ignore this rule for the last callback in a promise chain if their application logic or style guide deems it reasonable, ensuring the rule adds value without being overly prescriptive.avoid-new
bad
to fix
code snippets where checkData
function is refactored to utilize asyncValidateData
instead of creating a new promise.asyncValidateData
, developers are nudged towards leveraging abstractions that encapsulate the asynchronous logic.checkData
increases the risk of mishandling rejection or resolution states, which is mitigated when reusing asyncValidateData
.asyncValidateData
) instead of scattering new Promise instantiations across the codebase. This facilitates easier updates and optimizations to the asynchronous logic, as changes need to be made in fewer places.catch-or-return
.catch()
handler or returned for error handling further up the call chain, promoting robust error handling practices in asynchronous code, which is crucial to avoid unhandled promise rejections that can lead to runtime errors and application instability.allowThen
for error handling in a second .then()
method parameter, allowFinally
for using .finally()
as a termination method, and specifying alternative termination methods. This accommodates various coding styles and requirements while still enforcing error management.no-callback-in-promise
no-multiple-resolved
no-native
Promise
constructor when there could be simpler or more advanced abstractions available which could lead to clearer, more maintainable code.async/await
that can make asynchronous code easier to read and debug, especially in complex applications where understanding the flow of asynchronous operations is vital.Promise
states (pending, fulfilled, rejected) by encouraging patterns that might aid in avoiding common pitfalls such as unhandled promise rejections.no-nesting
no-new-statics
The rule specifically targets the incorrect use of new
with Promise
static methods such as Promise.resolve()
, Promise.reject()
, Promise.all()
, and Promise.race()
. These static methods are meant to be called directly on the Promise
constructor without creating a new instance.
It helps in preventing runtime errors that can occur from incorrectly instantiating Promise
static methods with new
. Since these methods are not constructor functions and are not meant to be called with new
, doing so can lead to unexpected behavior or errors in the code.
It enhances code readability and maintainability by ensuring that Promise
static methods are used correctly. Using new
with these methods is a common mistake among developers, especially those new to JavaScript’s Promise
API, and enforcing this rule can help catch such mistakes early.
The rule provides an automatic fix to incorrect usage by removing the new
keyword when applied to Promise
static methods. This not only helps in correcting the errors but also educates developers about the proper usage of Promise
static methods, promoting best practices within the codebase.
no-promise-in-callback
The rule helps maintain asynchronous code consistency by discouraging the mixing of callback patterns with promises within the same function, which prevents potential confusion regarding how error handling is managed and how the flow of asynchronous operations is understood. Mixing patterns can lead to harder-to-follow code and possible errors in handling asynchronous operations.
It encourages the use of modern JavaScript async/await syntax or thenable chains at a higher level rather than nesting promises inside callbacks. This leads to cleaner, more readable code, which is easier to debug and maintain.
Enforcing this rule helps in identifying and refactoring legacy code that uses mixed asynchronous patterns, promoting the adoption of newer, more efficient patterns of handling asynchronous operations. Such refactoring can lead to better performance and more consistent error handling.
It assists in avoiding potential issues related to the misuse of promises within callbacks, such as unhandled promise rejections or unexpected behavior due to the mixing of different asynchronous control flows, which can be hard to debug and lead to runtime errors.
no-return-in-finally
This rule ensures that the fulfillment or rejection of Promises is consistent by preventing a return
statement in a .finally()
block, which does not affect the promise’s outcome. This ensures developers do not mistakenly attempt to alter the promise result in a finally
block, which is not possible, leading to clearer and more predictable code behavior.
By disallowing return statements in finally
, it avoids confusion regarding the flow of promise resolution and rejection. Since finally
is used for cleanup after operations have completed, regardless of the outcome, injecting a return statement might misleadingly imply control over the promise’s resolution value or state which is not the case.
This rule emphasizes the separation of concerns by ensuring finally
blocks are used solely for cleanup purposes and not for returning values or affecting the promise chain. It promotes writing cleaner and more maintainable asynchronous JavaScript code by keeping the code responsible for resolving or rejecting promises separate from the code intended for cleanup activities.
Enforcing no return in finally
helps in debugging and code readability. When promises do not behave as expected, allowing return statements in finally
could lead to misleading interpretations of the code’s intention. By enforcing this rule, it becomes easier to track down where values are being resolved or rejected in the promise chain, reducing potential debugging time and improving maintainability.
no-return-wrap
Promise.resolve
or Promise.reject
within a Promise
chain, which can lead to code that is more complex and harder to read than necessary.Promise.resolve
or Promise.reject
creates an additional unnecessary promise that must be resolved or rejected, potentially impacting performance.param-names
prefer-await-to-callbacks
await/async
over traditional callback patterns, which can lead to clearer and more maintainable code structures.await/async
, it implicitly supports the handling of promises, which aligns with modern JavaScript practices and facilitates error handling using try/catch
blocks.prefer-await-to-then
This ESLint rule encourages the use of async/await
over then()/catch()/finally()
with promises, which can make code easier to read and maintain by reducing the complexity associated with promise chaining. The use of async/await
often leads to cleaner and more straightforward code that resembles synchronous code structures, improving readability.
The rule includes a check to ensure that it only reports .then()
, .catch()
, or .finally()
usage when not within an async
function or a generator function (yield
). This specificity helps prevent false positives in contexts where await
cannot be used directly, such as at the top-level scope in environments where top-level await
is not allowed.
By including a condition to ignore promises within yield
or await
expressions, the rule smartly targets only those promise chains that can and should be converted to async/await
. This focuses the developer’s attention on refactoring promise usages that will most benefit from async/await
syntax, without distracting with irrelevant recommendations.
The rule provides a clear message, “Prefer await to then()/catch()/finally().”, when a violation is detected. This direct feedback educates developers on modern JavaScript best practices, encouraging them to adopt async/await
in their code. Such specific guidance helps in codebase modernization efforts and aids in maintaining a uniform coding style across a project.
valid-params
Ensures the proper number of arguments are passed to Promise functions
resolve
, reject
, then
, etc., thereby promoting standard-compliant code which leads to more predictable behavior across different JavaScript environments.resolve
or reject
functions, which might lead to confusion regarding the outcome of the Promise or the parameters passed to the subsequent .then()
or .catch()
handlers.