You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The require-await rule tells the user to remove the async keyword when await is not used in the function. This is bad advise and will change the behaviour of the function. If you remove the async keyword then any error that is thrown in the function will no longer result in a rejected promise. A function that returns a Promise should always return a Promise, even in case of errors.
I think a better way to implement this rule would be to invert it. If the return type is Promise<...> then require the use of the async keyword.
Lint Name
require-await
Code Snippet
Simple example
// Linter tells me to remove the `async` keyword hereasyncfunctionfail(): Promise<void>{thrownewError("Failed");}
Slightly more realistic example
functionassertString(value: unknown): asserts value is string{if(typeofvalue!=="string"){thrownewError("Expected a string");}}// Linter tells me to remove async hereasyncfunctionsha256(input: unknown): Promise<ArrayBuffer|null>{// Assert that input is a stringassertString(input);// Call a method that would fail if the input is not a stringconstutf8=newTextEncoder().encode(input);// Return the result of an async function without await// It would be better if the linter told me to add an extra await here instead of removing async.returnglobalThis.crypto.subtle.digest("SHA-256",utf8);}asyncfunctionmain(){consthash=awaitsha256(123).catch(()=>null);console.log(hash);}
Expected Result
The linter should not warn about the async keyword.
Actual Result
The lister tells the user to remove the async keyword.
If you follow the advice of the linter and remove the async keywords, you will get an uncaught error.
The require-await rule tells the user to remove the
async
keyword whenawait
is not used in the function. This is bad advise and will change the behaviour of the function. If you remove theasync
keyword then any error that is thrown in the function will no longer result in a rejected promise. A function that returns a Promise should always return a Promise, even in case of errors.I think a better way to implement this rule would be to invert it. If the return type is
Promise<...>
then require the use of theasync
keyword.Lint Name
require-await
Code Snippet
Simple example
Slightly more realistic example
Expected Result
The linter should not warn about the
async
keyword.Actual Result
The lister tells the user to remove the
async
keyword.If you follow the advice of the linter and remove the async keywords, you will get an uncaught error.
Additional Info
Version
deno 1.39.1 (release, x86_64-apple-darwin)
v8 12.0.267.8
typescript 5.3.3
The text was updated successfully, but these errors were encountered: