Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle queries inside loop in node.js

Tags:

node.js

In the following code, the val variable value keeps changing due to asynchronous behavior and generates unexpected output i.e. before connection query for first loop is finished, var variable according to second loop and generates wrong output.What is the best way to handle queries in loop that avoid idiosyncrasies caused by asynchronous nature.

    var output = [];
    for ( var j = 0; j < someArr.length ; j++ ) {
         val = someArr[j];//some manipulation of someArr[j]
         connection.query( "select id from someTable where someCol = ?",val, function(err, rows, fields) {
             if ( err ) {
               console.log( err );
             } else {
               output.push( rows[0].someVal );//push query output to this variable
             }
       });
    }

console.log( output );//should contain output for all queries.

like image 944
johnnash Avatar asked Jan 24 '26 22:01

johnnash


1 Answers

Just use a closure to generate a temporary scope

var output;
    for ( var j = 0; j < someArr.length ; j++ ) {
         tVal = someArr[j];//some manipulation of someArr[j]
         (function(val){
           connection.query( "select id from someTable where someCol = ?",val, function(err, rows, fields) {
               if ( err ) {
                 console.log( err );
               } else {
                 output.push( rows[0].someVal );//push query output to this variable
               }
           });
         })(tVal);
    }

Theory

Execution Context

JavaScript is a single threaded language, meaning only one task can be executed at a time. When the JavaScript interpreter initially executes code, it first enters into a global execution context by default. Each invocation of a function from this point on will result in the creation of a new execution context.

The Scope Chain

For each execution context there is a scope chain coupled with it. The scope chain contains the variable object for every execution context in the execution stack. It is used for determining variable access and identifier resolution.

Explanation

The anonymous function help us to create a new scope "blocking" the value of tVal because when is executed a new scope is added to the scope chain containing val value. This new scope is a child of the parent scope in wich the for loop is executed, when the cycle continues tVal change but val is contained in the child scope and is safe from the variation.

like image 106
SharpEdge Avatar answered Jan 26 '26 12:01

SharpEdge