In this section, we will learn about synchronous and asynchronous programming in detail.
What is synchronous programming?
Synchronous programming is the programming in which the program executes in the order it is written and only one code block is executed at a time.
In another word, synchronous programs are blocking programs which block execution of code blocks below it until the current code block finishes its execution. There is no parallel execution in synchronous programming, code executes one-by-one.
In the above example, you can see the :
- first, the date object is stored in the variable
- Then in the next line date is logged out
- Then the loop finished its execution
- And finally program logs "Hello World!".
Every code block of the program runs in the same order it is written, hence it is the synchronous program.
What is asynchronous programming?
Asynchronous programming is the programming that executes in the order it is written until some asynchronous operation arrives.
When an asynchronous operation arrives then some block of code split from the program's normal execution and starts executing parallel to other blocks.
This means that instead of executing code blocks one by one it would be executing more than one block of code at a time.
In the above example you can see:
- Program first output is "Hello World!" which is the last line of the code
- Then it outputs the value of 'i' at an interval of 1 second
- And finally, after 2 seconds of the program outputs "Time out!".
You must have observed program is not executed in the sequence it is defined, it is non-blocking hence it is an asynchronous program.
But this is part of the story.
Synchronous codes make it really difficult for developers to work on the things around, suppose you have to download 100 images and store them somewhere in the device then you would not want to sit around it while it downloads all those to jump on the next task.
1. Callback functions
Callback functions are the functions that are passed as an argument to another function.
These callback functions can be served both in a synchronous and asynchronous way. It depends on the type of task, if there is an asynchronous operation then callback can be used in an asynchronous way.
For example, you need to fetch a file from the server then in this case you can use the callback function in an asynchronous way to do something once the file is fetched.
You will learn about the callback function in detail in the next section.
Just like callback function promise is used to resolve asynchronous operation but in a clean and better way.
Suppose you have to get a file from the server and then print the text content on the screen, then a possible code for fetting file and printing it could be as following getFile() function:
But this can be rewritten to return a promise and you would attach your callbacks to it instead.
Promises guarantees to run callback function after completion of the current run.