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

137
Views
Break/return in a for loop of async calls?

Currently my function looks like this:

const checkDynamoID = (dynamoClient, tableName) => {
  function runCheck(client, params, id) {
    return new Promise(resolve => {
      client.getItem(params, function(err, data) {
        if (err) {
          console.log("Error",  err);
        } else {
          if (data.Item === undefined) {
            console.log("Key available: " + id)
            resolve(id)
          } else {
            console.log("Key taken: " + id)
          }
        }
      });
    });
  }

  for (let i = 0; i < 10; i++) {
    const randomID = this.generateRandomBase64URL()
    const dynamoParams = {
      TableName: tableName, //TABLE_NAME
      Key: {
        'videoID': { S: randomID },
      }
    }

    const res = runCheck(dynamoClient, dynamoParams, randomID).then((result) => {
      console.log("Result: " + result)
      if (typeof result !== 'undefined') {
        console.log("Result: " + result)
        return result
      }
    })
  }

  return ""
}

My goal is to run runCheck, which makes async calls to a DynamoDB to check for an ID, in a for loop until it returns a valid ID. After that, I'd like to return or break out of the external for loop that encloses the call and return from checkDynamoID. However, I'm not sure how to do this; I tried running this and it does not return, even after generating a valid value (ie. typeof result !== 'undefined').

Is there a way of doing this?

about 4 years ago · Juan Pablo Isaza
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!