Simple way to synchronously execute code after setTimout() code is done
问题 I need a simple way to wait for setTimeout code to finish executing and then run the code that comes after setTimeout. Now the code after loop containing setTimout is executing before loop/setTimout is finished executing. for(let i = 0; i < 5; i++) { setTimeout(function(){ console.log(i); }, i*1000); } console.log("loop/timeout is done executing"); 回答1: setTimeout is by definition not synchronous - whatever you use to solve the issue will have to be asynchronous, there's no way around that.