Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!