|
| 1 | +# node/callback-return |
| 2 | +> require `return` statements after callbacks |
| 3 | +
|
| 4 | +The callback pattern is at the heart of most I/O and event-driven programming |
| 5 | + in JavaScript. |
| 6 | + |
| 7 | +```js |
| 8 | +function doSomething(err, callback) { |
| 9 | + if (err) { |
| 10 | + return callback(err); |
| 11 | + } |
| 12 | + callback(); |
| 13 | +} |
| 14 | +``` |
| 15 | + |
| 16 | +To prevent calling the callback multiple times it is important to `return` anytime the callback is triggered outside |
| 17 | + of the main function body. Neglecting this technique often leads to issues where you do something more than once. |
| 18 | + For example, in the case of an HTTP request, you may try to send HTTP headers more than once leading Node.js to `throw` |
| 19 | + a `Can't render headers after they are sent to the client.` error. |
| 20 | + |
| 21 | +## 📖 Rule Details |
| 22 | + |
| 23 | +This rule is aimed at ensuring that callbacks used outside of the main function block are always part-of or immediately |
| 24 | +preceding a `return` statement. This rule decides what is a callback based on the name of the function being called. |
| 25 | + |
| 26 | +### Options |
| 27 | + |
| 28 | +The rule takes a single option - an array of possible callback names - which may include object methods. The default callback names are `callback`, `cb`, `next`. |
| 29 | + |
| 30 | +#### Default callback names |
| 31 | + |
| 32 | +Examples of **incorrect** code for this rule with the default `["callback", "cb", "next"]` option: |
| 33 | + |
| 34 | +```js |
| 35 | +/*eslint callback-return: "error"*/ |
| 36 | + |
| 37 | +function foo(err, callback) { |
| 38 | + if (err) { |
| 39 | + callback(err); |
| 40 | + } |
| 41 | + callback(); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Examples of **correct** code for this rule with the default `["callback", "cb", "next"]` option: |
| 46 | + |
| 47 | +```js |
| 48 | +/*eslint callback-return: "error"*/ |
| 49 | + |
| 50 | +function foo(err, callback) { |
| 51 | + if (err) { |
| 52 | + return callback(err); |
| 53 | + } |
| 54 | + callback(); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +#### Supplied callback names |
| 59 | + |
| 60 | +Examples of **incorrect** code for this rule with the option `["done", "send.error", "send.success"]`: |
| 61 | + |
| 62 | +```js |
| 63 | +/*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/ |
| 64 | + |
| 65 | +function foo(err, done) { |
| 66 | + if (err) { |
| 67 | + done(err); |
| 68 | + } |
| 69 | + done(); |
| 70 | +} |
| 71 | + |
| 72 | +function bar(err, send) { |
| 73 | + if (err) { |
| 74 | + send.error(err); |
| 75 | + } |
| 76 | + send.success(); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Examples of **correct** code for this rule with the option `["done", "send.error", "send.success"]`: |
| 81 | + |
| 82 | +```js |
| 83 | +/*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/ |
| 84 | + |
| 85 | +function foo(err, done) { |
| 86 | + if (err) { |
| 87 | + return done(err); |
| 88 | + } |
| 89 | + done(); |
| 90 | +} |
| 91 | + |
| 92 | +function bar(err, send) { |
| 93 | + if (err) { |
| 94 | + return send.error(err); |
| 95 | + } |
| 96 | + send.success(); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### Known Limitations |
| 101 | + |
| 102 | +Because it is difficult to understand the meaning of a program through static analysis, this rule has limitations: |
| 103 | + |
| 104 | +* *false negatives* when this rule reports correct code, but the program calls the callback more than one time (which is incorrect behavior) |
| 105 | +* *false positives* when this rule reports incorrect code, but the program calls the callback only one time (which is correct behavior) |
| 106 | + |
| 107 | +#### Passing the callback by reference |
| 108 | + |
| 109 | +The static analysis of this rule does not detect that the program calls the callback if it is an argument of a function (for example, `setTimeout`). |
| 110 | + |
| 111 | +Example of a *false negative* when this rule reports correct code: |
| 112 | + |
| 113 | +```js |
| 114 | +/*eslint callback-return: "error"*/ |
| 115 | + |
| 116 | +function foo(err, callback) { |
| 117 | + if (err) { |
| 118 | + setTimeout(callback, 0); // this is bad, but WILL NOT warn |
| 119 | + } |
| 120 | + callback(); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +#### Triggering the callback within a nested function |
| 125 | + |
| 126 | +The static analysis of this rule does not detect that the program calls the callback from within a nested function or an immediately-invoked function expression (IIFE). |
| 127 | + |
| 128 | +Example of a *false negative* when this rule reports correct code: |
| 129 | + |
| 130 | +```js |
| 131 | +/*eslint callback-return: "error"*/ |
| 132 | + |
| 133 | +function foo(err, callback) { |
| 134 | + if (err) { |
| 135 | + process.nextTick(function() { |
| 136 | + return callback(); // this is bad, but WILL NOT warn |
| 137 | + }); |
| 138 | + } |
| 139 | + callback(); |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +#### If/else statements |
| 144 | + |
| 145 | +The static analysis of this rule does not detect that the program calls the callback only one time in each branch of an `if` statement. |
| 146 | + |
| 147 | +Example of a *false positive* when this rule reports incorrect code: |
| 148 | + |
| 149 | +```js |
| 150 | +/*eslint callback-return: "error"*/ |
| 151 | + |
| 152 | +function foo(err, callback) { |
| 153 | + if (err) { |
| 154 | + callback(err); // this is fine, but WILL warn |
| 155 | + } else { |
| 156 | + callback(); // this is fine, but WILL warn |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +## 🔎 Implementation |
| 162 | + |
| 163 | +- [Rule source](../../lib/rules/callback-return.js) |
| 164 | +- [Test source](../../tests/lib/rules/callback-return.js) |
0 commit comments