Skip to content

Commit

Permalink
Update no-then.md
Browse files Browse the repository at this point in the history
The 'improved code' after linting was unnecessarily complex introducing a named variable across multiple lines for no reason which made it look better to keep using `then`. A bit surprising.

Previous catch example was code which didn't differentiate between failing case (which is now a deliberate null) and undefined case (which is often the result of an error).
  • Loading branch information
cefn authored Feb 3, 2024
1 parent e77f49c commit 72c0daa
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions docs/rules/no-then.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,39 @@ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/asy

👎 Examples of **incorrect** code for this rule:

```js
function countData(url) {
return downloadData(url).then(data => {
return data.length
})
}
```

```js
function getProcessedData(url) {
return downloadData(url).catch(e => {
console.log('Error occurred!', e)
return null;
})
}
```

👍 Examples of **correct** code for this rule:

```js
async function countProcessedData(url) {
const data = await downloadData(url);
return data.length
```
```js
async function getProcessedData(url) {
let v
try {
v = await downloadData(url)
return await downloadData(url)
} catch (e) {
console.log('Error occurred!', e)
return
console.log('Error occurred!', e);
return null;
}
return v
}
```
Expand Down

0 comments on commit 72c0daa

Please sign in to comment.