Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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);
                }
            });
            }
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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);
    }
  }
...
...
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda