2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
wobsoriano
  • 12,348
  • 24
  • 92
  • 162

3 Answers3

2

I suggest you to use callback function as below. I took an API call to represent your Database request and I put the condition to loop until the string has the character v inside. With that it will work like a charm.

function loadData(callback){
  $.ajax({url: "https://helloacm.com/api/random/?n=10", success: function(response){
    callback(response);
  }});
}

function checkData(response){
  if(response.includes("w")){
    console.log(response, "good");
  } else {
    console.log(response, "bad");
    loadData(checkData);
  }
}


function register(){
  loadData(checkData);
}

register();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Jonathan Gagne
  • 4,241
  • 5
  • 18
  • 30
  • whats the benefit of using callbacks here? – Eduard Jacko Oct 15 '18 at 07:05
  • not really sure what you mean by 2 different interaction, but once you use callbacks then is really and I mean really hard to refactor your code to promises. Callbacks are good if you want to register a method to be called outside your flow. For example `onRenderElement(callback)` it wouldn't make sense to await for element because I need to render anytime when data change out of my control/flow. However, if you controlling the flow then Promises is way to go. Async and await is just another way how to write a promise. – Eduard Jacko Oct 15 '18 at 07:25
  • hows `return await mixCharOrderUserHappy(checkdata)` is less intuitive? There is a term which was solved by promises `callback hell` and after that `promise hell` which was solved by async. You can imagine the `callback hell` as chained reaction `doA({}, doB({}, doC({}, doD({})),secondCallback(whereTheHellImInjected())))` And sometimes you need to call such a chain outside of any of those function. At least if you want clean API. – Eduard Jacko Oct 15 '18 at 07:41
1

Yes this works, the entire selling point of async await is that you can make promise based code look like regular imperative constructs (such as while loops) just by adding the await keyword whenever you call out to another async function.

Performance wise, you could obviously benefit from generating the random ID on the server so that you always get an ID which is known to be unique in a single call. This is probably not a problem in practice as having more than 1 collision is likely to be very rare if the space of IDs is sufficiently large.

Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46
0

infinity do while is good when you have multiple cases when you need to change the condition of loop. In your case its simple. If found record, return otherwise do it again. However, the function name should represent it function. In your case is register, but actually you retrieving random record. Performance? Well, you are not really saving much here. You have couple async calls which will blocks your script until resolved. Example without dowhile https://stackblitz.com/edit/js-bgyman

Eduard Jacko
  • 1,974
  • 1
  • 16
  • 29