0

how to execute for loop one by one when large data process it does not work properly I want to develop such same logic using node.js.

result1 is like this

[
  {
    "FNODE_ID": "30015",
    "END_TIME": "2021-02-03",
    "START_TIME": 57,
    "C1": "[2206.888,2206.859,0.028999999999996362]",
    "TOTAL": 6
  },
  {
    "FNODE_ID": "30015",
    "END_TIME": "2021-02-03",
    "START_TIME": 56,
    "C1": "[2206.854,2206.815,0.03899999999975989]",
    "TOTAL": 8
  },
  {
    "FNODE_ID": "30015",
    "END_TIME": "2021-02-03",
    "START_TIME": 55,
    "C1": "[2206.809,2206.766,0.04300000000012005]",
    "TOTAL": 9
  },
  {
    "FNODE_ID": "30015",
    "END_TIME": "2021-02-03",
    "START_TIME": 54,
    "C1": "[2206.76,2206.728,0.032000000000152795]",
    "TOTAL": 7
  }
]

this is nested for loop

 var fvalue1 = {}
     for (var j = 0; j < result1.length; j++) {
          for (var node in nodeParameterObj) {
            if (result1[j]['FNODE_ID'] == node) {
              if ('undefined' == typeof fvalue1[node]) {
                fvalue1[node] = {}
              }
              for (var parameter in nodeParameterObj[node]) {
                if ('undefined' == typeof fvalue1[node][parameter]) {
                  fvalue1[node][parameter] = {}
                }
                if (result1[j][nodeParameterObj[node][parameter]] != null) {
                  var a = JSON.parse(result1[j][nodeParameterObj[node][parameter]])
                  a.push(0, 0, 0);
                  a.push(result1[j]['TOTAL']);
                  fvalue1[node][parameter] = a.toString();
                }
              }
            }
          }     
        }

final output is

{
  "30015": {
    "C1": {
      "54": "2206.76,2206.728,0.032000000000152795,0,0,0,7",
      "55": "2206.809,2206.766,0.04300000000012005,0,0,0,9",
      "56": "2206.854,2206.815,0.03899999999975989,0,0,0,8",
      "57": "2206.904,2206.859,0.04500000000007276,0,0,0,9"
    }
  }
}

but it does not handle a large amount of data I am trying like this

  var processResult = function(callback){
            async.forEach(result1, function (item, callback){ 
              console.log(item);      
              callback();       
          }, function(err) {
              console.log('iterating done');
          });        
        }
processResult(function(err, users) {
      resolve(fvalue1); 
  });
  

But the console does not print any value

Mayur
  • 3
  • 3
  • 2
    All this code is synchronous so it's not clear what you're trying to do differently with your loops. Please explain what exactly you want help with? – jfriend00 Feb 03 '21 at 07:48
  • Now check the updated question – Mayur Feb 03 '21 at 09:05
  • `async.forEach` is not asynchronous at all if you're immediately calling `callback()` synchronously. – Bergi Feb 03 '21 at 11:34

1 Answers1

0

Because javascript is single threaded running, complex synchronous code can block all other code (within the same node process) from running. One easy way around this is to run chunks of the code inside a timeout. setTimeout is an asynchronous function, this will give the event loop a change to check for other async requests to deal with. You can use a timeout of 0 milliseconds and it will still produce a break if needed.

const fvalue1 = {}
for (var j = 0; j < result1.length; j++) {
  for (var node in nodeParameterObj) {
    setTimeout(function () {
      if (result1[j]['FNODE_ID'] == node) {
        if ('undefined' == typeof fvalue1[node]) {
          fvalue1[node] = {}
        }
        for (const parameter in nodeParameterObj[node]) {
          if ('undefined' == typeof fvalue1[node][parameter]) {
            fvalue1[node][parameter] = {}
          }
          if (result1[j][nodeParameterObj[node][parameter]] != null) {
            const a = JSON.parse(result1[j][nodeParameterObj[node][parameter]])
            a.push(0, 0, 0)
            a.push(result1[j]['TOTAL'])
            fvalue1[node][parameter] = a.toString()
          }
        }
      }
    }, 0)
  }
}
spirift
  • 2,994
  • 1
  • 13
  • 18