Wednesday 7 June 2017

looping in node js takes the last value only

I have certain functions need to be called for each value of a array. But the loop is running for the last value only always. Below is how my code is structured.

    function1(data)
    .then(data){
    array1.foreach(function(value1){
         value1.foreach(function(value2){
         var reqData = {};
         reqData.data = data;
         reqData.attr1 = value2.attr1;
         reqData.attr2 = value2.attr2;

         function2(reqData.data)
            .then(function3)
            .then(function4)
            .then(function (success){
                  console.log(success);
            });
         });
      });
    };

  function2(data){
  //---
   return Q.resolve(data)
  }

  function3(data){
   //-----
   return Q.resolve(data)
  }

  function4(data){
   //------
   return Q.resolve(data)
  }

When I debug I am able to see only the last value of the value2 array is only running for all the functions. I want all the functions need to be run for each value of the array.

Thanks in advance.



via user1733952

No comments:

Post a Comment