Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
How to change this iteration into a async function?

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);
                }
            });
            }
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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);
    }
  }
...
...
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda