0

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?

Victor M
  • 603
  • 4
  • 22
  • Use `async`/`await`. Your current code does `return` only from the `then` callback – Bergi Mar 18 '22 at 04:08
  • Could you elaborate a little more? I use `then` because I want the for loop to wait until the return value from the Promise is resolved and processed. – Victor M Mar 18 '22 at 04:26
  • …and that's what it *doesn't* do when you use `then`. It would work if you used `await` instead. – Bergi Mar 18 '22 at 04:31
  • Was trying to follow this: https://stackoverflow.com/questions/27759593/how-do-i-wait-for-a-promise-to-finish-before-returning-the-variable-of-a-functio Do you mind sharing an example? My understanding was that `await` will return a Promise, which can resolve asynchronously. So it won't stall the for loop until the Promise is resolved. – Victor M Mar 18 '22 at 17:18
  • `await` (and `async`) makes the `checkDynamoID` immediately return a promise, while stalling the `for` loop until the `await`ed promise (`runCheck(…)`) is settled. Then when the loop has finished and the function `return`s, the promise is resolved with the result. – Bergi Mar 18 '22 at 17:37
  • Right, but then wouldn't I have to make `checkDynamoID` async as well? Then the code that calls `checkDynamoID` would get a Promise rather than a bool, which I don't want either. Ideally `checkDynamoID` should return a value at the end of the execution without promises. – Victor M Mar 18 '22 at 20:25
  • Yes, `checkDynamoID` must return a promise, there's no way around that (regardless whether using `async`/`await` or not): it does asynchronous stuff, so it's result must be asynchronous. You cannot synchronously block, waiting for a promise to resolve. You cannot return a boolean, the value is not immediately known. – Bergi Mar 19 '22 at 00:37

0 Answers0