Is there a way to achieve the setTimeout() function's behavior in a for-loop? Take a look at this code snippet:
function hello() {
for (let index = 0; index < 3; index++) {
setTimeout(function () {
console.log('What\'s up!')
}, 3000)
console.log('Yo')
}
}
hello()
The output of this code is:
Yo
Yo
Yo
What's up!
What's up!
What's up!
How can we modify it to log like this:
Yo
What's up(after 3 sec)
Yo
What's up(after 3 sec)
Yo
What's up(after 3 sec)
Thank you for your assistance.