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

341
Vistas
NodeJS return data from for() function

This might be a very basic question but I've never done it and I can't make it so far, so I'm gonna asking it here!

I have a function in separate js file and it supposed to return messages from for loop data but not sure where the return has to go!

this is my code:

function test(x,y) {
    for(let i in y) {
        if(y[i].value == 'x') {
            return bot.message(y[i]);
        }
        sleep(1500);
    }
}

this doesn't return anything.

So what I'm trying to do here is to send multiple messages from my loop and put 1.5 seconds sleep between each message.

What did I do wrong?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Can you please try this...

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
const bot = {
    message: (value) => {
        console.log('[Bot]', '[Received]', value);
    }
};
async function test(x, y) {
    for (let i in y) {
        if (y[i].value === x) {
            bot.message(y[i]);
        }
        await sleep(1500);
        console.log('Woke Up!!!');
    }
}
const y = [{ value: 'a' }, { value: 'b' }, { value: 'a' }];
const x = 'a';
test(x, y);

Of course, imagination involved.

about 4 years ago · Juan Pablo Isaza 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