Skip to content

Commit 9baaddc

Browse files
committed
slight addition to no-callback-in-promise docs
1 parent fa81d93 commit 9baaddc

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

__tests__/no-callback-in-promise.js

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ ruleTester.run('no-callback-in-promise', rule, {
1717
'function thing(callback) { callback() }',
1818
'doSomething(function(err) { callback(err) })',
1919

20+
// TODO: support safe callbacks (see #220)
21+
// 'whatever.then((err) => { process.nextTick(() => cb()) })',
22+
// 'whatever.then((err) => { setImmediate(() => cb())) })',
23+
// 'whatever.then((err) => setImmediate(() => cb()))',
24+
// 'whatever.then((err) => process.nextTick(() => cb()))',
25+
// 'whatever.then((err) => process.nextTick(cb))',
26+
// 'whatever.then((err) => setImmediate(cb))',
27+
2028
// arrow functions and other things
2129
'let thing = (cb) => cb()',
2230
'doSomething(err => cb(err))',

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Avoid calling `cb()` inside of a `then()` or `catch()` (no-callback-in-promise)
22

3-
As a general rule, callbacks should never be directly invoked inside a [Promise.prototype.then()] or [Promise.prototype.catch()] method. That's because your callback may be unintentionally be invoked twice. Take the following example:
3+
As a general rule, callbacks should never be directly invoked inside a [Promise.prototype.then()] or [Promise.prototype.catch()] method. That's because your callback may be unintentionally be invoked twice. It also can be confusing to mix paradigms.
4+
5+
Take the following example:
46

57
```js
68
function callback(err, data) {
@@ -45,9 +47,9 @@ Your output will now look like the following:
4547
Callback got called with: null data
4648
```
4749

48-
Finally, if your callbacks have a Node.js signature (i.e. `callback(err, data)`), consider using [nodeify] for simplifying your code.
50+
Finally, if your callbacks have a Node.js signature (i.e. `callback(err, data)`), consider using [util.promsify] for promisifying your callback code instead of combining the approaches.
4951

50-
[nodeify]: https://www.npmjs.com/package/nodeify
52+
[util.promisify]: https://nodejs.org/dist/latest/docs/api/util.html#utilpromisifyoriginal
5153
[Promise.prototype.then()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
5254
[Promise.prototype.catch()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
5355
[setImmediate()]: https://nodejs.org/docs/latest-v14.x/api/timers.html#timers_setimmediate_callback_args

0 commit comments

Comments
 (0)