I have a an async function that checks if the id
already exists in a table.
async function generateIdentifier () {
try {
let exists;
let id;
do {
id = someRandomStringGenerator();
const email = await Database.find({id});
if (email.length > 0) {
exists = true;
} else {
exists = false;
}
} while (exists);
return id;
} catch (e) {
throw e;
}
}
With the code above, the find method will return an array. If the array is empty, means no id is found. When an id is found, it should generate a new one until id is unique.
Also, yes this works though performance wise, are there better options with doing things like this?