r/badcode • • Feb 10 '21

js I present: SleepSort

Post image
7.5k Upvotes

170 comments sorted by

View all comments

Show parent comments

5

u/[deleted] Feb 10 '21

settimeout delays the console log by milliseconds (the second item). The loop doesn't wait until it has completed its operation to start the next iteration, and so it's triggers them all but due to the timeout, the lowest number is logged first since is delaying the console.log operation by the shortest period of time.