Home > front end >  How to change this iteration into a async function?
How to change this iteration into a async function?

Time:02-15

This code isn't working probably, because the connection to mongo isn't established fast enough. So I thought that I have to change it to a asnyc function. But definetly doesn't know where to start. I'm new to Node and Mongo

left
Julian Graber 900 1
windowsdemo 673 3
laptopDemo 640 4
IpadDemo 628 5

It should looks like that, but mongo updates it that every rank is the same digit.

// function for giving rank
function giveRank(arrayArg,resultArg){
  // declaring and initilising variables
    let rank = 1;
    prev_rank = rank;
    position = 0;
    // displaying the headers in the console
    console.log('\n-------OUR RESULTS------\n');
    console.log('Name | Mark | Position\n');
    // looping through the rank array
    for (i = 0; i < arrayArg.length ; i   ) {
            /*
            If it is the first index, then automatically the position becomes 1.
            */
            if(i == 0) {
                position = rank;
            Ranking.find({name: arrayArg[i]}).then((data) => {
                if(data){
                updateRank(bla, bla, bla)
                }else{
                newRank(bla,bla,bla);
                }
            });
            
            /*
            if the value contained in `[i]` is not equal to `[i-1]`, increment the `rank` value and assign it to `position`.
            The `prev_rank` is assigned the `rank` value.
            */
            } else if(arrayArg[i] != arrayArg[i-1]) {
            rank   ;
            position = rank;
            prev_rank = rank;
            Ranking.find({name: arrayArg[i]}).then((data) => {
                if(data){
                updateRank(bla, bla, bla)
                }else{
                newRank(bla,bla,bla);
                }
            });
            
            /*
            Otherwise, if the value contained in `[i]` is equal to `[i-1]`,
            assign the position the value stored in the `prev_rank` variable then increment the value stored in the `rank` variable.*/
            } else {
                position = prev_rank;
                rank   ;
                Ranking.find({name: arrayArg[i]}).then((data) => {
                if(data){
                updateRank(bla, bla, bla)
                }else{
                newRank(bla,bla,bla);
                }
            });
            }
    }
}

CodePudding user response:

To convert a promise-chain-based asynchronous call into an async/await call, you only need to do a couple things:

  1. Convert the parent function into an async function by using the async keyword, like so: function myFunction (...) {} turns into async function myFunction (...) {}

  2. Use the await keyword in front of your function call, like so: doStuff(...) turns into await doStuff(...)

  3. Convert the parameter to your first .then() callback to the return value of the async call, like so: doStuff().then((data) => {}) turns into let data = await doStuff()

  4. Move the body of the .then() callback below the assignment of the response, like so: doStuff().then((data) => { console.log(data) }) turns into let data = await doStuff(); console.log(data);

So in your example, converting one of the functions would look like so:

async function giveRank(arrayArg,resultArg){
...
...
  if(i == 0) {
    position = rank;
    let data = await Ranking.find({name: arrayArg[i]})
    if(data){
      updateRank(bla, bla, bla)
    }else{
      newRank(bla,bla,bla);
    }
  }
...
...
}
  • Related