Skip to content

Commit 602d825

Browse files
authoredJul 24, 2024
docs: supply missing docs (#503)
**What is the purpose of this pull request?** - [x] Documentation update **What changes did you make? (Give an overview)** Supply missing doc pages. Draws heavily again from #199. Also: - uses latest Node URLs Fixes #214.
1 parent 197ae4e commit 602d825

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed
 

‎README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ or start with the recommended rule set:
132132

133133
[util.callbackify]:
134134
https://nodejs.org/docs/latest/api/util.html#utilcallbackifyoriginal
135-
[util.promisify]:
136-
https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
135+
[util.promisify]: https://nodejs.org/api/util.html#util_util_promisify_original
137136
[@aaditmshah]: https://github.com/aaditmshah
138137
[@macklinu]: https://github.com/macklinu
139138
[@xjamundx]: https://github.com/xjamundx

‎docs/rules/avoid-new.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,46 @@
55

66
<!-- end auto-generated rule header -->
77

8-
[util.promisify]:
9-
https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
8+
Avoid using `new Promise` in favour of utility libraries or
9+
`Promise.resolve`/`reject`.
10+
11+
## Rule Details
12+
13+
Creating promises using `new Promise` can be used to promisify Node-style
14+
callbacks. However, you can use Node's [`util.promisify`]() instead.
15+
16+
`new Promise` is also sometimes misused to wrap a value or error into a promise.
17+
However, this can be done more concisely and clearly with `Promise.resolve` and
18+
`Promise.reject`.
19+
20+
Examples of **incorrect** code for this rule:
21+
22+
```js
23+
function promisifiedFn(arg) {
24+
return new Promise((resolve, reject) => {
25+
callbackStyleFn(arg, (error, result) =>
26+
error ? reject(error) : resolve(result)
27+
)
28+
})
29+
}
30+
31+
new Promise((resolve, reject) => resolve(1))
32+
new Promise((resolve, reject) => reject(new Error('oops')))
33+
```
34+
35+
Examples of **correct** code for this rule:
36+
37+
```js
38+
import util from 'util'
39+
const promisifiedFn = util.promisify(callbackStyleFn)
40+
41+
Promise.resolve(1)
42+
Promise.reject(new Error('oops'))
43+
```
44+
45+
## When Not To Use It
46+
47+
If you are creating a utility library without [util.promisify]() or do not want
48+
to be notified when using `new Promise`, you can safely disable this rule.
49+
50+
[util.promisify]: https://nodejs.org/api/util.html#util_util_promisify_original

‎docs/rules/no-callback-in-promise.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ callback code instead of combining the approaches.
6868
[promise.prototype.catch()]:
6969
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
7070
[setimmediate()]:
71-
https://nodejs.org/docs/latest-v14.x/api/timers.html#timers_setimmediate_callback_args
71+
https://nodejs.org/docs/latest/api/timers.html#timers_setimmediate_callback_args
7272
[process.nexttick()]:
73-
https://nodejs.org/docs/latest-v14.x/api/process.html#process_process_nexttick_callback_args
73+
https://nodejs.org/docs/latest/api/process.html#process_process_nexttick_callback_args
7474
[settimeout()]:
7575
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
7676

‎docs/rules/no-promise-in-callback.md

+30
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,33 @@
44
`recommended`.
55

66
<!-- end auto-generated rule header -->
7+
8+
Discourages the use of promises inside callbacks.
9+
10+
## Rule Details
11+
12+
Promises and callbacks are different ways to handle asynchronous code and should
13+
not be mixed.
14+
15+
Examples of **incorrect** code for this rule:
16+
17+
```js
18+
doSomething((err, val) => {
19+
if (err) console.error(err)
20+
else doSomethingElse(val).then(console.log)
21+
})
22+
```
23+
24+
Examples of **correct** code for this rule:
25+
26+
```js
27+
promisify(doSomething)()
28+
.then(doSomethingElse)
29+
.then(console.log)
30+
.catch(console.error)
31+
```
32+
33+
## When Not To Use It
34+
35+
If you do not want to be notified when using promises inside of callbacks, you
36+
can safely disable this rule.

0 commit comments

Comments
 (0)