
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
62 lines
1.4 KiB
Plaintext
62 lines
1.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
An exception (including ``++reject++``) thrown by a promise will not be caught by a nesting ``++try++`` block, due to the asynchronous nature of execution. Instead, use ``++catch++`` method of ``++Promise++`` or wrap it inside ``++await++`` expression.
|
|
|
|
|
|
This rule reports ``++try-catch++`` statements containing nothing else but call(s) to a function returning a ``++Promise++`` (thus it's less likely that ``++catch++`` is intended to catch something else than ``++Promise++`` rejection).
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,javascript]
|
|
----
|
|
function runPromise() {
|
|
return Promise.reject("rejection reason");
|
|
}
|
|
|
|
function foo() {
|
|
try { // Noncompliant, the catch clause of the 'try' will not be executed for the code inside promise
|
|
runPromise();
|
|
} catch (e) {
|
|
console.log("Failed to run promise", e);
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,javascript]
|
|
----
|
|
function foo() {
|
|
runPromise().catch(e => console.log("Failed to run promise", e));
|
|
}
|
|
|
|
// or
|
|
async function foo() {
|
|
try {
|
|
await runPromise();
|
|
} catch (e) {
|
|
console.log("Failed to run promise", e);
|
|
}
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Consider using 'await' for the promise(s) inside this 'try' or replace it with 'Promise.prototype.catch(...)' usage(s).
|
|
|
|
|
|
=== Highlighting
|
|
|
|
'try' keyword
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|