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?
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.