WeSt
WeSt

Reputation: 929

Node js promises with nested loops

I was banging my head around this now for while but I can't solve this. From Module_A I call a method in Module_B with promises. In this method I use nested each loops an I have a problem by using the right syntax.

I try to break this down. In this example the array locations[] is always empty and I can't resolve it to Module_A back.

Module_A:

app.listen(server_connection.port, () => {
console.log('Server started on port ' + server_connection.port);
getRegions() // This works
  .then(function(response){
    return constructionsiteParser.constructionsiteParser(response); // here is the problem to receive the array from Module_B 
  })
  .then(function(locations){
    console.log(locations.length);
  })
  .catch(function(msg){
    console.error(msg);
  });
});

Module_B:

var locations = [];

exports.constructionsiteParser = function constructionsiteParser(xmlArr){
var promise = new Promise (function(resolve, reject){
  getLocations(xmlArr)
    .then(function(locations){
      console.log(locations); //locations is empty and can't resolve it to Module_A
      if(objArr > 0){
        resolve(locations);
      }else{
        reject("Locations array is empty");
      }
    })
 });
 return promise;
}


let getLocations = function(xmlArr){
return new Promise(function(resolve, reject){
      var $ = cheerio.load(xmlArr[0]);
      $('situation').each( function(){
          parseImportantInfos($, this)
            .then(function(obj){
              locations.push(obj);
            })
      });
    resolve(locations); // the problem must be somewhere here. This resolve will be executed bevor the eachloop from above is done
})
}


let parseImportantInfos = function($, record){
 return new Promise (function(resolve, reject){
   $(record).find('situationRecord').each( function(i){
      //do some stuff
      if(startLocationCode != '' && endLocationCode != '' ){
        Promise.all([
          //do some stuff
        ])
        .then( values =>{
          // do some stuff
        })
        .then(function(obj){
          resolve(obj);
        })
        .catch((err) =>{
           reject("There was an error while parsing the xml");
           console.log('err', err.stack);
       });
      }
   });
   });
  };

What I want is to return the complete locations array back to Module_A

Upvotes: 2

Views: 1009

Answers (1)

TKoL
TKoL

Reputation: 13912

instead of

let getLocations = function(xmlArr){
    return new Promise(function(resolve, reject){
          var $ = cheerio.load(xmlArr);
          $('situation').each( function(){
              parseImportantInfos($, this)
                .then(function(obj){
                  locations.push(obj);
                })
          });
        resolve(locations); // the problem must be somewhere here. This resolve will be executed bevor the eachloop from above is done
    })
}

you'd want to use something like

let getLocations = function(xmlArr){
    return new Promise(function(resolve, reject){
        var $ = cheerio.load(xmlArr);
        var promiseList = [];
        $('situation').each( function(){
            var newPromise = parseImportantInfos($, this)
            .then(function(obj){
                locations.push(obj);
            })
            promiseList.push(newPromise)
        });

        Promise.all(promiseList).then(function(){
            resolve(locations);
        })
    })
}

Upvotes: 4

Related Questions