重构DRY的节点代码(请勿重复您自己)

时间:2018-09-06 14:47:44

标签: node.js refactoring

具有以下代码

exports.list_tum_izinler = function (req, res) {
    var _user = toknHelper.getUserFromToken(req);
    izinler.list_Izinler(_user.username, sendResult);

    function sendResult(data) {
        if (data instanceof Error) {
            res.status(500).send(data.message);
            return;
        }
        res.status(200).send(data);
    }
}

exports.Izin_Counts= function (req, res) {
    var _user = toknHelper.getUserFromToken(req);
    izinler.Count_Izinler(_user.username, sendResult);

    function sendResult(data) {
        if (data instanceof Error) {
            res.status(500).send(data.message);
            return;
        }
        res.status(200).send(data);
    }

这里尝试将代码重构为DRY,我想提取此部分

 function sendResult(data) {
        if (data instanceof Error) {
            res.status(500).send(data.message);
            return;
        }
        res.status(200).send(data);
    }

作为外部函数,并向该函数发送数据和res参数。这里的要点是data参数从函数返回,而res参数是一个外部参数,因此我如何发送带参数的res参数。

1 个答案:

答案 0 :(得分:0)

创建其他文件... myFunction.js:

const sendResult = ({ res, data }) => {
  if (data instanceof Error) {
    res.status(500).send(data.message);
    return;
  }
  res.status(200).send(data);
};

module.exports = sendResult;

然后,将其输入到您当前的代码中:

const sendResult = require('./myFunction');

exports.list_tum_izinler = function (req, res) {
    var _user = toknHelper.getUserFromToken(req);
    izinler.list_Izinler(_user.username, sendResult);
}

您需要意识到您正在使用此函数作为回调,因此,如果您想将此函数提供给izinler.Count_Izinler(_user.username, sendResult),则需要修改该函数以正确地向其提供响应。还需要将响应传递给该函数,并在需要时将其提供给回调:

const Count_Izinler(username, res, callback) => {
  // dunno what this function does... but maybe it gets data somewhere
  let data = 'some data';
  callback({res, data});
}

但是,如果要执行此操作,则可能只需要将该函数放入实现本身中即可,而不是在高阶函数中并将其作为回调删除:

const sendResult = require('./path/to/myFunction');

const Count_Izinler(username, res) => {
  // dunno what this function does... but maybe it gets data somewhere
  let data = 'some data';
  return sendResult({res, data});
}

const list_izinler(username, res) => {
  // dunno what this function does... but maybe it gets data somewhere
  let data = 'some data';
  return sendResult({res, data});
}

然后,您将像这样调用这些函数:

izinler.list_Izinler(_user.username, res);

但是,说实话,这主要是造成一堆代码混乱,并使人们感到困惑。它变得难以维护。您未来的自我会讨厌您这样做。我建议您只从dataCount_Izinler返回list_Izinler,然后将其输入到sendResult中,就像这样:

const sendResult = require('./myFunction');

exports.list_tum_izinler = function (req, res) {
    var _user = toknHelper.getUserFromToken(req);
    const data = izinler.list_Izinler(_user.username); // <-- modify this function to only take user name, and RETURN whatever it does, which is the "data"
    return sendResult({res, data});
}

exports.Izin_Counts= function (req, res) {
    var _user = toknHelper.getUserFromToken(req);
    const data = izinler.Count_Izinler(_user.username); // <-- modify this function to only take user name, and RETURN whatever it does, which is the "data"
    return sendResult({res, data});
}

很遗憾,您省略了其他代码段,例如list_IzinlerCount_Izinler,以使某人完全为您提供100%准确的答案。如果您想获得更清晰的答案,请提供其他功能的更多代码段。

编辑:使用异步/等待。.

exports.list_tum_izinler = async function (req, res) { // <-- specify the function as async...
    var _user = toknHelper.getUserFromToken(req);
    const data = await izinler.list_Izinler(_user.username); // <-- use "await" before the invocation.  This function must return a promise in order for this to work.  If it does not, list_Izinler needs to be modified to return a promise... mostly a trivial thing to do...
    return sendResult({res, data});
}