0

I have a code block containing if else and most of the code in if and else is same, but due to one function call with callback, I have to quite a same code in both if and else block. I know a function can be created for this purpose but it will require me to pass too many paramaters. Here is the code block:

if (results[0].register_type == 'D') {
                sessionData.register_type = results[0].register_type;
                UserModel.updategcm_id(req.body, function (err, result) {
                    UserSessionModel.createSessionToken(sessionData, function (err, result, token) {
                        if (err) {
                            res.status(400).send(self.createResponse({}, {
                                success: false,
                                message: err.message
                            }));
                            return false;
                        }
                        res.status(200).send(self.createResponse({
                            token: token,
                            userid: results[0].id
                        }, {
                                success: true,
                                message: "User has logged in successfully"
                            }));
                        console.log("User has logged in successfully...\n");
                    });
                });
            } else {
                UserSessionModel.createSessionToken(sessionData, function (err, result, token) {
                    if (err) {
                        res.status(400).send(self.createResponse({}, {
                            success: false,
                            message: err.message
                        }));
                        return false;
                    }
                    res.status(200).send(self.createResponse({
                        token: token,
                        userid: results[0].id
                    }, {
                            success: true,
                            message: "User has logged in successfully"
                        }));
                    console.log("User has logged in successfully...\n");
                });
            }
4
  • 1
    Use a function with a parameter. Commented Jul 1, 2016 at 9:56
  • Why don't you just create a function and pass err, results and token to it? Commented Jul 1, 2016 at 9:58
  • @Seonixx: Actually he already is creating that very function. (twice). Commented Jul 1, 2016 at 10:09
  • @Bergi ah yes - my bad. Commented Jul 1, 2016 at 10:11

2 Answers 2

1

I think that the best solution is to move the repeated function to a function declaration and just pass it as callback. Your code would change to:

if (results[0].register_type == 'D') {
    sessionData.register_type = results[0].register_type;
    UserModel.updategcm_id(req.body, function (err, result) {
        UserSessionModel.createSessionToken(sessionData, validateResponse);
    });
} else {
    UserSessionModel.createSessionToken(sessionData, validateResponse);
}

function validateResponse(err, result, token) {
    if (err) {
        res.status(400).send(self.createResponse({}, {
            success: false,
            message: err.message
        }));
        return false;
    }
    res.status(200).send(self.createResponse({
        token: token,
        userid: results[0].id
    }, {
        success: true,
        message: "User has logged in successfully"
    }));
    console.log("User has logged in successfully...\n");
}

The code is much more clean now.

You could also factor out the whole createSessionToken call into a function, but you'd have to be careful with passing sessionData, self and res (depending on their respective scope).

Sign up to request clarification or add additional context in comments.

2 Comments

You might need to bind self depending on the scope in which you put validateResponse.
You are right, but it depends of the rest of the code. I will edit the response, thanks!
0

You may use async module, so that you don't need to repeat the function.

2 Comments

Can you point out where exactly I should look in the async documentation: caolan.github.io/async/docs.html ? I am not sure what should I search in the docs.
Do npm install --save async, then require the module in your server file. After that follow the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.