Skip to content

Commit 932123e

Browse files
authored
Merge pull request #188 from jp7837/Issue187
prefer-await-to-then should flag promise.catch/finally as well
2 parents 120117f + 6378bb9 commit 932123e

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-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

@@ -36,6 +37,14 @@ ruleTester.run('prefer-await-to-then', rule, {
3637
code:
3738
'async function a() { hey.then(function() { }).then(function() { }) }',
3839
errors: [{ message }, { message }]
40+
},
41+
{
42+
code: 'function foo() { hey.catch(x => {}) }',
43+
errors: [{ message }]
44+
},
45+
{
46+
code: 'function foo() { hey.finally(x => {}) }',
47+
errors: [{ message }]
3948
}
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: 4 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,11 @@ 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 (node.property && (node.property.name === 'then' || node.property.name === 'catch' || node.property.name === 'finally')) {
4444
context.report({
4545
node: node.property,
46-
message: 'Prefer await to then().'
46+
message: 'Prefer await to then()/catch()/finally().'
4747
})
4848
}
4949
}

0 commit comments

Comments
 (0)