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?