7 - https://github.com/maxogden/art-of-node#callbacks
8 - https://web.archive.org/web/20171224042620/https://docs.nodejitsu.com/articles/errors/what-are-the-error-conventions/
12 This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n).
14 The callback pattern is at the heart of most I/O and event-driven programming
18 function doSomething(err, callback) {
26 To prevent calling the callback multiple times it is important to `return` anytime the callback is triggered outside
27 of the main function body. Neglecting this technique often leads to issues where you do something more than once.
28 For example, in the case of an HTTP request, you may try to send HTTP headers more than once leading Node.js to `throw`
29 a `Can't render headers after they are sent to the client.` error.
33 This rule is aimed at ensuring that callbacks used outside of the main function block are always part-of or immediately
34 preceding a `return` statement. This rule decides what is a callback based on the name of the function being called.
38 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`.
40 ### Default callback names
42 Examples of **incorrect** code for this rule with the default `["callback", "cb", "next"]` option:
47 /*eslint callback-return: "error"*/
49 function foo(err, callback) {
59 Examples of **correct** code for this rule with the default `["callback", "cb", "next"]` option:
64 /*eslint callback-return: "error"*/
66 function foo(err, callback) {
76 ### Supplied callback names
78 Examples of **incorrect** code for this rule with the option `["done", "send.error", "send.success"]`:
83 /*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/
85 function foo(err, done) {
92 function bar(err, send) {
102 Examples of **correct** code for this rule with the option `["done", "send.error", "send.success"]`:
107 /*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/
109 function foo(err, done) {
116 function bar(err, send) {
118 return send.error(err);
128 Because it is difficult to understand the meaning of a program through static analysis, this rule has limitations:
130 * *false negatives* when this rule reports correct code, but the program calls the callback more than one time (which is incorrect behavior)
131 * *false positives* when this rule reports incorrect code, but the program calls the callback only one time (which is correct behavior)
133 ### Passing the callback by reference
135 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`).
137 Example of a *false negative* when this rule reports correct code:
140 /*eslint callback-return: "error"*/
142 function foo(err, callback) {
144 setTimeout(callback, 0); // this is bad, but WILL NOT warn
150 ### Triggering the callback within a nested function
152 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).
154 Example of a *false negative* when this rule reports correct code:
157 /*eslint callback-return: "error"*/
159 function foo(err, callback) {
161 process.nextTick(function() {
162 return callback(); // this is bad, but WILL NOT warn
169 ### If/else statements
171 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.
173 Example of a *false positive* when this rule reports incorrect code:
176 /*eslint callback-return: "error"*/
178 function foo(err, callback) {
180 callback(err); // this is fine, but WILL warn
182 callback(); // this is fine, but WILL warn
187 ## When Not To Use It
189 There are some cases where you might want to call a callback function more than once. In those cases this rule
190 may lead to incorrect behavior. In those cases you may want to reserve a special name for those callbacks and
191 not include that in the list of callbacks that trigger warnings.