From 72c0daa8ad101fc22144c1255aec66b99e1879c8 Mon Sep 17 00:00:00 2001 From: Cefn Hoile Date: Sat, 3 Feb 2024 10:50:24 +0000 Subject: [PATCH 1/3] Update no-then.md 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). --- docs/rules/no-then.md | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/docs/rules/no-then.md b/docs/rules/no-then.md index 1d377230..c336d5a7 100644 --- a/docs/rules/no-then.md +++ b/docs/rules/no-then.md @@ -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 } ``` From d8417846237c0cf01e5bdef257915a96ebba9486 Mon Sep 17 00:00:00 2001 From: Ned Schwartz Date: Mon, 5 Feb 2024 15:51:57 -0500 Subject: [PATCH 2/3] closing bracket --- docs/rules/no-then.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-then.md b/docs/rules/no-then.md index c336d5a7..f010fb2d 100644 --- a/docs/rules/no-then.md +++ b/docs/rules/no-then.md @@ -35,7 +35,7 @@ function getProcessedData(url) { async function countProcessedData(url) { const data = await downloadData(url); return data.length -``` +} ```js async function getProcessedData(url) { From 8abd830c7d77f056f4936967aea07429afc0b718 Mon Sep 17 00:00:00 2001 From: Ned Schwartz Date: Mon, 5 Feb 2024 15:54:23 -0500 Subject: [PATCH 3/3] closing fence --- docs/rules/no-then.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/rules/no-then.md b/docs/rules/no-then.md index f010fb2d..3937bbf9 100644 --- a/docs/rules/no-then.md +++ b/docs/rules/no-then.md @@ -36,6 +36,7 @@ async function countProcessedData(url) { const data = await downloadData(url); return data.length } +``` ```js async function getProcessedData(url) {