0

I want my code to be executed synchronously in the same order I call the 'asyncFunc' function. The first call needs to finished before executing the next call and so on.

I would like the below sample program to output:

type => slow
type => medium
type => fast

How do I accomplish that?

var asyncFunc = function( type ) {

    if( type == 'slow')
    {
        $timeout( function(){ console.log('type => slow'); }, 10000);
    }
    else if( type == 'medium')
    {
        $timeout( function(){ console.log('type => medium'); }, 5000);
    }
    else if( type == 'fast')
    {
        $timeout( function(){ console.log('type => fast'); }, 1);
    }
}

asyncFunc('slow');
asyncFunc('medium');
asyncFunc('fast');
rikard
  • 121
  • 2
  • 4
  • 1
    Does this answer your question? [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) – Igor Mar 27 '20 at 15:25
  • See the aforementioned [suggested duplicate](https://stackoverflow.com/q/14220321/1260204). Asynchronous calls are a common and critical building block in writing/designing an application. It is critical that you understand how to work with asynchronous calls in javascript, and by extension typescript. Understanding these core concepts will help you become a better programmer and also ensure you do not keep "stubbing your toe" on the same problem. – Igor Mar 27 '20 at 15:25
  • If you search on `javascript promise chaining` you will also get a lot of answers like https://stackoverflow.com/q/29853578/1260204 and https://stackoverflow.com/q/35711084/1260204 – Igor Mar 27 '20 at 15:29

0 Answers0