Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
Codeblock works, the return doesn't

Not even sure how to phrase this exactly.

I have the two functions you can see below. checkDescription works and returns as expected and then rejects the promise. The second block "checkAwsVsSgs" also executes as expected, but never rejects the promise or even executes the test console.log section.

// Library Module, Public functions
module.exports = {


  checkSG: function(block) { 
    return new Promise((resolve, reject) => {
      let found = false;

      Object.keys(awsSecurityGroups).forEach( (key)=> {
        let group = awsSecurityGroups[key];
        if (block.local_action.name === group.GroupName) {
          found = true;

          if (!checkDescription(group, block)) { reject("Description doesn't match.") }
          if (!checkAwsVsSgs(group, block)) { console.log("!!!!! I'm never called"); reject("Found rules in aws that are not in sgs") }

        }
      });

      resolve(found);
    });
  },

};

// Private functions
function checkDescription(group, block) {
  return group.Description === block.local_action.description
}

function checkAwsVsSgs(group, block) { // Look for rules that are in AWS but not sgs
  group.IpPermissions.forEach( (awsRule) => {
    let foundRuleMatch = false;
    block.local_action.rules.forEach ( (blockRule) => {
      //console.log("Checking FromPort %s vs %s and ToPort %s vs %s for %s", awsRule.FromPort, blockRule.from_port, awsRule.ToPort, blockRule.to_port, group.GroupName)
      if (awsRule.ToPort === blockRule.to_port && awsRule.FromPort === blockRule.from_port) { foundRuleMatch = true }
    });
    if (!foundRuleMatch) {
      console.log("Error: Could not find matching rule for %s FromPort: %s ToPort: %s", group.GroupName, awsRule.FromPort, awsRule.ToPort);
      return false;
    }
  });
  return true;
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It's because you're always returning true in checkAwsVsSgs. The return false; that you have inside the forEach is for the forEach function, not for the outside function. You need to do something like this:

function checkAwsVsSgs(group, block) { // Look for rules that are in AWS but not sgs
  var found = true;
  group.IpPermissions.forEach( (awsRule) => {
    let foundRuleMatch = false;
    block.local_action.rules.forEach ( (blockRule) => {
      //console.log("Checking FromPort %s vs %s and ToPort %s vs %s for %s", awsRule.FromPort, blockRule.from_port, awsRule.ToPort, blockRule.to_port, group.GroupName)
      if (awsRule.ToPort === blockRule.to_port && awsRule.FromPort === blockRule.from_port) { foundRuleMatch = true }
    });
    if (!foundRuleMatch) {
      console.log("Error: Could not find matching rule for %s FromPort: %s ToPort: %s", group.GroupName, awsRule.FromPort, awsRule.ToPort);
      found = false;
      return;
    }
  });
  return found;
}

Also, if you want to test whether some element in the array passes certain condition, consider using Array.prototype.some() or Array.prototype.every() for all elements.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda