0
\$\begingroup\$

EDIT:

The code i provide includes a asynchron task in each interation, therefor i need the recursive function, my question is how to beautify the code and apply the proper use.

I want to:

  • Remove the processTask(); function call and let it call itself instead.
  • Choose the best function declaration.

    connection.query(queryString, function (err, rows, fields) {
     if (err) return throwSQLError(err, req, res, connection);
     var rowLength = rows.length;
     var taskIteration = 0;
     // Iterate over all tasks with a recursive function
     var processTask = function () {
     if (taskIteration < rowLength) {
     // I do asynch stuff and call those two lines below
     // in the callback of the asynch operation.
     taskIteration++;
     processTask();
     }
     else {
     // Send JSON to result
     res.json(inputArray);
     // Close MySQL connection
     connection.end();
     }
     };
     processTask();
    });
    

I'm thinking something like this might work:

function processTask(){ // Which function creator to use?
 if (taskIteration < rowLength) {
 // I do asynch stuff and call those two lines below
 // in the callback of the asynch operation.
 taskIteration++;
 processTask();
 }
 else {
 // Send JSON to result
 res.json(inputArray);
 // Close MySQL connection
 connection.end();
 }
}(); // instead of the explicit function call, call the function calls itself
asked Jan 28, 2016 at 10:07
\$\endgroup\$
2
  • \$\begingroup\$ Could you supply all of your code? Answers may likely be different given that you're excluding the closure which contains connection, res, taskIteration and rowLength. \$\endgroup\$ Commented Jan 28, 2016 at 10:22
  • \$\begingroup\$ Those closure variables are essentially dispensable, they are part of a node.js mySQL connection and a express.js path and two ints defining for the loop. I basically just want to iterate over my results recursively, but i feel like this solution is not the very best. But if you really think that'd help i'll edit my answer. \$\endgroup\$ Commented Jan 28, 2016 at 10:27

1 Answer 1

1
\$\begingroup\$

Why not just

connection.query(queryString, function (err, rows, fields) {
 if (err) return throwSQLError(err, req, res, connection);
 rows.forEach(function (row) {
 // do what you need to with each row;
 });
 /* or maybe even
 var result = rows.map(function (row) {
 // do what you need to with row and return it
 });
 and then do something with result
 */
 res.json(inputArray); // Not clear from the question where inputArray has come from 
 connection.end();
}
answered Jan 28, 2016 at 15:29
\$\endgroup\$
1
  • \$\begingroup\$ I think my initial question was articulated really bad, i edited in hope of improvement. \$\endgroup\$ Commented Jan 28, 2016 at 15:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.