Skip to content

Commit 7fa5b56

Browse files
jp7837brettz9
authored andcommitted
prefer-await-to-then should flag promise.catch/finally as well
1 parent 7ac8796 commit 7fa5b56

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ or start with the recommended rule set:
9090
| [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method | :bangbang: | :wrench: |
9191
| [`no-return-in-finally`][no-return-in-finally] | Disallow return statements in `finally()` | :warning: | |
9292
| [`valid-params`][valid-params] | Ensures the proper number of arguments are passed to Promise functions | :warning: | |
93-
| [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()` for reading Promise values | :seven: | |
93+
| [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values | :seven: | |
9494
| [`prefer-await-to-callbacks`][prefer-await-to-callbacks] | Prefer async/await to the callback pattern | :seven: | |
9595

9696
**Key**

__tests__/prefer-await-to-then.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ const ruleTester = new RuleTester({
88
},
99
})
1010

11-
const message = 'Prefer await to then().'
11+
const message = 'Prefer await to then()/catch()/finally().'
1212

1313
ruleTester.run('prefer-await-to-then', rule, {
1414
valid: [
1515
'async function hi() { await thing() }',
1616
'async function hi() { await thing().then() }',
1717
'async function hi() { await thing().catch() }',
1818
'a = async () => (await something())',
19+
'a = async () => (try { await something() } catch { somethingElse() })',
1920
'something().then(async () => await somethingElse())',
2021
],
2122

@@ -37,5 +38,13 @@ ruleTester.run('prefer-await-to-then', rule, {
3738
'async function a() { hey.then(function() { }).then(function() { }) }',
3839
errors: [{ message }, { message }],
3940
},
41+
{
42+
code: 'function foo() { hey.catch(x => {}) }',
43+
errors: [{ message }],
44+
},
45+
{
46+
code: 'function foo() { hey.finally(x => {}) }',
47+
errors: [{ message }],
48+
},
4049
],
4150
})

docs/rules/prefer-await-to-then.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Prefer `await` to `then()` for reading Promise values (prefer-await-to-then)
1+
# Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values (prefer-await-to-then)
22

33
#### Valid
44

@@ -33,4 +33,14 @@ function exampleTwo() {
3333
.then(doSomethingElseAsync)
3434
.catch(errors)
3535
}
36+
37+
function exampleThree() {
38+
return myPromise
39+
.catch(errors)
40+
}
41+
42+
function exampleFour() {
43+
return myPromise
44+
.finally(cleanup)
45+
}
3646
```

rules/prefer-await-to-then.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Rule: prefer-await-to-then
3-
* Discourage using then() and instead use async/await.
3+
* Discourage using then()/catch()/finally() and instead use async/await.
44
*/
55

66
'use strict'
@@ -39,11 +39,16 @@ module.exports = {
3939
return
4040
}
4141

42-
// if you're a then expression then you're probably a promise
43-
if (node.property && node.property.name === 'then') {
42+
// if you're a then/catch/finally expression then you're probably a promise
43+
if (
44+
node.property &&
45+
(node.property.name === 'then' ||
46+
node.property.name === 'catch' ||
47+
node.property.name === 'finally')
48+
) {
4449
context.report({
4550
node: node.property,
46-
message: 'Prefer await to then().',
51+
message: 'Prefer await to then()/catch()/finally().',
4752
})
4853
}
4954
},

0 commit comments

Comments
 (0)