callback-return

此规则在 ESLint v7.0.0 中已弃用。请使用 eslint-plugin-node 中的相应规则。

回调模式是 JavaScript 中大多数 I/O 和事件驱动编程的核心。

function doSomething(err, callback) {
    if (err) {
        return callback(err);
    }
    callback();
}

为了防止多次调用回调,在主函数体之外触发回调时,return 很重要。忽略此技术通常会导致您多次执行某项操作的问题。例如,在 HTTP 请求的情况下,您可能会尝试多次发送 HTTP 标头,从而导致 Node.js 向 throw 发送 Can't render headers after they are sent to the client. 错误。

规则详情

此规则旨在确保在主功能块之外使用的回调始终是 return 语句的一部分或紧接在 return 语句之前。此规则根据被调用函数的名称决定什么是回调。

选项

该规则采用单个选项 - 可能的回调名称数组 - 其中可能包括对象方法。默认回调名称为 callbackcbnext

默认回调名称

1usau4ITNzPbSW1XK9odV99asreU1qGDCN2tDW6sPrpB/1y0vGLZnSWzeJuBXD4zPXCsiT/Y7ntteFEtq1l76xaD8zWHBnFq1ZQpRqk7BMXfz2Q2tCfY6DSuem/CKH3m

/*eslint callback-return: "error"*/

function foo(err, callback) {
    if (err) {
        callback(err);
    }
    callback();
}

1usau4ITNzPbSW1XK9odV99asreU1qGDCN2tDW6sPrpB/1y0vGLZnSWzeJuBXD4zPXCsiT/Y7ntteFEtq1l764PqlRVniGc8fAPdJBv3ad+bMrk6kB//k+dsE3llqXey

/*eslint callback-return: "error"*/

function foo(err, callback) {
    if (err) {
        return callback(err);
    }
    callback();
}

提供的回调名称

elyW6tcYPuTEl7RyG3oLfVLxcUA6/VCJHHhP0e5BbpKjj/K33BaxKU30GZiauCBANAKJsdVj73Ilnw6K8tbFL9/SsRHW0FMfIiS5LFergWJp+rkT+LHm+Yoni0f76trXsRyBXBfhs6ikFeCKAnG3OQ==

/*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/

function foo(err, done) {
    if (err) {
        done(err);
    }
    done();
}

function bar(err, send) {
    if (err) {
        send.error(err);
    }
    send.success();
}

elyW6tcYPuTEl7RyG3oLfVLxcUA6/VCJHHhP0e5BbpKjj/K33BaxKU30GZiauCBANAKJsdVj73Ilnw6K8tbFL9/SsRHW0FMfIiS5LFergWLI3iIw97bKotTgJN35cc9EEp+ru4sdsMA9N8oGTZ0NpQ==

/*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/

function foo(err, done) {
    if (err) {
        return done(err);
    }
    done();
}

function bar(err, send) {
    if (err) {
        return send.error(err);
    }
    send.success();
}

已知限制

w0/ILIBinkG4dhIcAsaAp24GRB+uwkfrZ8LLi2mEg62CT/rbWQ7Ws4wBIvyCBxjf6rHYp3o8ykX2oHoX+rAyOhVxBPzMrXt7q8/y17pbqCPR/LMM8l164OiS1LToTNm8

    aijl1caD50T7+tt3mxkwoXRiHU9PTqrI+c+iPLO9iibZT2eOMKm59+ek6EmoUJHvIvvDVOJVaGL0KdB7OVWF8+OXtBH4dGU9gDDrdf/3S+hLqanOYDQnEcGs9d4gCcLcItSP7igTtwZOTTi+bmjZ1DAqbVuzQVqUnVtnzynCChC55NHv+SSR/BvoXJJvscCS2JYk1HBLcA/OM1eJy814Q2K1Le+WMMrGQzbJc68f0fR1tdRkM7JczKO7bxLqsZXIQ3PymUA9iDk6SKpklpGotJ2ycUtyySvwYF96J+QK/XLTt2um6jB3WGLhmHw1LpKz2VrGwfdrPN0M7jMAmNWR1w==

通过引用传递回调

/5631bd/cmX99vCR3Ba46rIPpfiT3SH0wyXSatQNHmQam5iYNzen46LtqYQDKfBpFxhwWjjEM+Lm/KKgjsdldP/Si2ajySRwkhIHk7sXWE7PXSZAc19uR9+IUR1kY4d+EkD2SbmxKzKtElJGRik9a8rj/XuAmB0T7liZperE+dM=

QtZuSxSGpAOnUM4ysmylASlEyhtx+W8tk/B+5HLqNVimCyTX5Lz2TQH4+terDAxDqIanhVi1jVgcDKVB6cOnQw==

/*eslint callback-return: "error"*/

function foo(err, callback) {
    if (err) {
        setTimeout(callback, 0); // this is bad, but WILL NOT warn
    }
    callback();
}

在嵌套函数中触发回调

+JFTwKpzLA9jiGydBOq+AmL8tOszMP5St2qe+QYzAo5uXmCMEFw7/F5bPQ9S2INHWeiqotVrWTUziBI7SIXLE0gt0CIrEtl0PTBuSg8Bt8pLWGpiQQISo/EgdtVQW3B3ic7Jqenr5qZMJ5Gh++9XxB9LUlduvASBPL2xR1OIBDU=

QtZuSxSGpAOnUM4ysmylASlEyhtx+W8tk/B+5HLqNVimCyTX5Lz2TQH4+terDAxDqIanhVi1jVgcDKVB6cOnQw==

/*eslint callback-return: "error"*/

function foo(err, callback) {
    if (err) {
        process.nextTick(function() {
            return callback(); // this is bad, but WILL NOT warn
        });
    }
    callback();
}

if/else 语句

pSrCXfiTHzSvKHexvifFVNokjGMKN3VJXJFX5p8rKrdn42+4KXYZ4zF0TXEI03UTGay3YX9SQ3KhME5jpy/7+n8XhVnJqJGWGIn0bu83/em9tiS3LUJthFbKNOAJq2H6qDbWV7G4zV95HgOgKl2vCkXjXy2KRANjs34X9H01SQg=

eS8/l+X6HrTpULgxAN/IQfkxDwpO/ax4taRZJt9Vd8pmyB97+pJv3LwulRPuDX7/ZxDSsPaQqoF4uCiGI0TiAw==

/*eslint callback-return: "error"*/

function foo(err, callback) {
    if (err) {
        callback(err); // this is fine, but WILL warn
    } else {
        callback();    // this is fine, but WILL warn
    }
}

何时不使用

E/lQuon4n2bJlDQFs5pgqyKYEX+MGd9Yi1iCPWxPrIH2pJ6mjtMibfWj9L5hMiLQzbkzKmefzk7i5RntoyvGxn+Er4UOf0xh4m9KYu0drmVfhPWoZXXln688mPrU2hSUn4hTsDlA/Qz/hsQaR128L/vQEiDcxHurNbChDivq9WGLuECgFg1kOUCGOHBqTGaYdhc0CmlsP0//Joav5Ato3PBvWwNTfkuRK9zS3LQtQJPnhfp82q9BlKxc8exIzotkZOiukkZrLooMcb3QXlhoEz06pnl8B2QQxy/ki5jrXEb9gUv1q9c7TGNSoNe4e6y4sRie02BwT63ezxY9IBt+er0Okc/93Jd5yZfRJBczJjA=