-2

I've 3 functions, f1(), f2(),f3() like below,

 function f1(){
    setTimeout(function(){
    console.log("Hi 1")
    },3000);
    }

    function f2(){
    setTimeout(function(){
    console.log("Hi 2")
    },2000);
    }


        function f3(){
        setTimeout(function(){
        console.log("Hi 3")
        },1000);
        }
    f1();f2();f3();

when I execute this , I got result like Hi3, Hi2, Hi1, but what I expected result is Hi1, Hi2,Hi3. I try this using promise and callback functions, but I didn't resolve this, can any one help me how to do this. Thank you

  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Taki Jul 05 '18 at 12:46
  • possible duplicate with https://stackoverflow.com/questions/16026942/how-do-i-chain-three-asynchronous-calls-using-jquery-promises – madjaoue Jul 05 '18 at 12:46
  • You are giving timeout milliseconds in the wrong order. `f1` should have 1000, `f2` 2000 and so on. – cornacchia Jul 05 '18 at 12:46
  • 3
    Why do you expect that ordering? Your first delay is 3 seconds, the second is 2 seconds, and the third is 1 second. – Pointy Jul 05 '18 at 12:46
  • 1
    Because 3seconds last longer than 2seconds ?! – Jonas Wilms Jul 05 '18 at 12:47
  • Please show us *how* you tried to use callbacks and/or promises – Bergi Jul 05 '18 at 13:12
  • var ispromise = new Promise(function(resolve,reject){ var clean = true; if(clean){ resolve('clean'); }else{ reject(); } }) ispromise.then(function(clean){ f1(); }).then(function(clean){ f2(); }).then(function(clean){ f3(); }); – Sreekanth Dabbeeru Jul 05 '18 at 13:28

1 Answers1

0
   function f1(){
        setTimeout(function(){
        console.log("Hi 1")
        },1000);
        }

    function f2(){
    setTimeout(function(){
    console.log("Hi 2")
    },1000);
    }


        function f3(){
        setTimeout(function(){
        console.log("Hi 3")
        },1000);
        }
    f1();f2();f3();

Make the timeout period same else even if you call the f1 first it will wait 3 seconds to show pop up meanwhile f2 and f3 executes