JavaScript Using Async/Await on multiple settimeout using async await,using async await c#,using async await

To understand async/await, I am trying to display a console message once settimeout runs and expires. How do I fix my code below? I have 5 settimeout function and each should display respective message once finished.

function t1(){
    setTimeout(() => {
    console.log("1")
    }, 1000);
}

function t2(){
    setTimeout(() => {
    console.log("2")
    }, 2000);
}

function t3(){
    setTimeout(() => {
    console.log("3")
    }, 3000);
}

function t4(){
    setTimeout(() => {
    console.log("4")
    }, 4000);
}

function t5(){
    setTimeout(() => {
    console.log("5")
    }, 5000);
}

async function main(){
    await t1();
    console.log("1sec done");
    await t2();
    console.log("2sec done");
    await t3();
    console.log("3sec done");
    await t4();
    console.log("4sec done");
    await t5();
    console.log("Yay! I am all done");
}
main();
Answer:1

You should use Promises

function t1(){
    return new Promise(function(resolve, reject) {
      setTimeout(() => {
        console.log("1");
        resolve();
      }, 1000);
   });
}

function t2(){
    return new Promise(function(resolve, reject) {
      setTimeout(() => {
        console.log("2");
        resolve();
      }, 1000);
   });
}

function t3(){
    return new Promise(function(resolve, reject) {
      setTimeout(() => {
        console.log("3");
        resolve();
      }, 1000);
   });
}

function t4(){
    return new Promise(function(resolve, reject) {
      setTimeout(() => {
        console.log("4");
        resolve();
      }, 1000);
   });
}

function t5(){
    return new Promise(function(resolve, reject) {
      setTimeout(() => {
        console.log("5");
        resolve();
      }, 1000);
   });
}

async function main(){
    await t1();
    console.log("1sec done");
    await t2();
    console.log("2sec done");
    await t3();
    console.log("3sec done");
    await t4();
    console.log("4sec done");
    await t5();
    console.log("Yay! I am all done");
}
main();
Answer:2

You are doing two mistakes

  • First you are using await before a calling a function t1,t2,t3.... await should be used on Promises.
  • You are passing 1000,2000,... to setTimeout(). You should create a function which returns a Promise which will be resolved after 1 second
  • Use await before the promise returned by that function

let afterOne = (num) => new Promise(res => {
    setTimeout(()=>{
      //log the number passed to function after 1000 ms
      console.log(num);
      //resolve the current promise so then next setTimeout could be set
      res();
   },1000)
  })
async function main(){
    /*This loop does same as
       await afterOne(0);
       await afterOne(1);
       await afterOne(2)
       await afterOne(3);
       await afterOne(4);
    */
    for(let i = 0;i<5;i++){
      await afterOne(i)
    }
}
main();
Answer:3

Hi i'm using Jquery Upload plugin. I've got this problem: when i upload the first file this work fine,a single request is sent. But if i select a second file and i upload it,two request are sent and ...

Hi i'm using Jquery Upload plugin. I've got this problem: when i upload the first file this work fine,a single request is sent. But if i select a second file and i upload it,two request are sent and ...

  1. when do we choose another president

I've tried to render a simple component but cant seem to make it work. <Col md={{ span: 16, offset: 4 }} xs={{ span: 20, offset: 2 }}> {props.language === "en" ? <englishReport /> : &...

I've tried to render a simple component but cant seem to make it work. <Col md={{ span: 16, offset: 4 }} xs={{ span: 20, offset: 2 }}> {props.language === "en" ? <englishReport /> : &...

I have a scenario to get a output like the following. 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 When the input is 3 it would give 8 combinations as above. If the input is 4, there would be 16 ...

I have a scenario to get a output like the following. 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 When the input is 3 it would give 8 combinations as above. If the input is 4, there would be 16 ...

  1. recursive loop python
  2. recursive loop detected in template string
  3. recursive loop java
  4. recursive loop javascript
  5. recursive loop definition
  6. recursive loop c++
  7. recursive loop r
  8. recursive loop matlab
  9. recursive loop example
  10. recursive loop array javascript
  11. recursive loop in sql server
  12. recursive loop in oracle sql
  13. recursive loop php
  14. recursive loop knime
  15. recursive loop js
  16. recursive loop in angular 6
  17. recursive loop in jquery
  18. recursive loop in pl sql
  19. recursive loop start knime
  20. recursive loop through json

I'm using the Twitter Typeahead plugin: https://twitter.github.io/typeahead.js/ Here is the code I'm using: $(document).ready( function() { var bestPictures = new Bloodhound({ datumTokenizer:...

I'm using the Twitter Typeahead plugin: https://twitter.github.io/typeahead.js/ Here is the code I'm using: $(document).ready( function() { var bestPictures = new Bloodhound({ datumTokenizer:...