Skip to content
Advertisement

How to print an array updated by setTimeout? [closed]

The task is to write a function to reverse an array of numbers and print out each number every 2 seconds. Also print the reversed array in the end.

I wrote the following to achieve this, but it is a bad method, since I need to calculate the number of seconds in each setTimeout and also take extra steps to determine whether the for loop has reached the last iteration.

Is there a direct way to pause 2 seconds after each iteration and then print the reversed array in a synchronous way?

JavaScript

Advertisement

Answer

Is there a better way to return an updated value after setTimeout?

Promisify setTimeout on its own, then use async/await:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement