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

145
Vistas
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
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