I have the following scenario to implement in node.
- Printing random words inside a loop.
- There is an asynchronous function to call for that (printRandomWord).
- The function has a delay of 0.5 seconds to get the response.
- I have to call this function inside a loop from 1 to 20.
- It takes 10 seconds to print 20 random words since the printRandomWord has a 0.5 s delay.
What I want to do is, print 20 random words in less than 1 second.
I was told that there is a way to do that, but I couldn't find the solution yet.
This is my code.
const printWords = async () => {
for (let x = 1; x <= 100; x++) {
console.log(await printRandomWord());
}
};