Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!