JavaScript 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");
Answer: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.

The best way to achieve something like this is to use Promises instead, calling Promise.all on an array of the created promises:

(async () => {
  await Promise.all(Array.from(
    { length: 5 },
    (_, i) => new Promise(res => setTimeout(() => {
      console.log(i);
      res();
    }, i * 1000))
  ));
  console.log("loop/timeout is done executing");
})();
Answer:2

I have a ul with items coming from the loop, and then extra li after that. <ul> <todo-item v-for="(todo,index) in todos" v-bind:todo="todo" :key="index" /> <li :key='"new_item"'>...

I have a ul with items coming from the loop, and then extra li after that. <ul> <todo-item v-for="(todo,index) in todos" v-bind:todo="todo" :key="index" /> <li :key='"new_item"'>...

I have a select tag with 4 options. I want to get a particular option clicked when the page loads and a method should called because of click event. Here what I have tried so far: HTML <select>...

I have a select tag with 4 options. I want to get a particular option clicked when the page loads and a method should called because of click event. Here what I have tried so far: HTML <select>...

  1. click event page
  2. click event on page load
  3. click event on page jquery
  4. click event reload page
  5. click event after page load
  6. click event fires on page load
  7. click event in html page
  8. stop button click event page refresh
  9. trigger click event on page load jquery
  10. trigger click event on page load
  11. call click event on page load jquery
  12. page click event javascript
  13. jquery click event after page load
  14. trigger click event after page load
  15. javascript click event on page load
  16. jquery click event reloads page
  17. angular click event reloads page
  18. button click event on page load c#
  19. button click event reload page
  20. prevent click event on page refresh

I created my own method which basically capitalizes the first alphabet of every word in a string. However, I am getting this Uncaught TypeError: Cannot read property 'split' of undefined error. Where ...

I created my own method which basically capitalizes the first alphabet of every word in a string. However, I am getting this Uncaught TypeError: Cannot read property 'split' of undefined error. Where ...

I have a submit button, now I want result like this: When I click on first time it will be run action 1. When I click on second time it will be run action 2. When I click on n time it will be run ...

I have a submit button, now I want result like this: When I click on first time it will be run action 1. When I click on second time it will be run action 2. When I click on n time it will be run ...

  1. multi action button apk
  2. multi action button
  3. multi action button ux
  4. multi action button pro apk
  5. multi action home button pro apk
  6. multi action home button apk
  7. multi action home button
  8. multi action home button pro
  9. multi action home button pro 2.2.8 apk
  10. multi floating action button android
  11. multi action home button pro apk download
  12. multi action home button pro version download
  13. multi action home button apk download
  14. multi action home button app
  15. multi floating action button flutter
  16. multi action home button mod apk
  17. bootstrap multi action button
  18. multi action home button pro mod apk
  19. multi action home button download
  20. multi action home button full version