What the hell is “ASYNC” ??
Most of the time, we also don’t get the real problem but the solution.
To understand Asynchronous property, we will go through synchronous property and its difference from asynchronous property.First of all, let’s start with a funny example in a simple way.
I'll try to explain as simply as I can so you'll (hopefully) remember:
Synchronous Execution :
When a task is executed synchronously, you wait for a task to be completed before moving on to another task. One task depends on the end of another.
My boss is a busy man. He tells me to write the code. I tell him: Fine. I get started and he's watching me like a vulture, standing behind me, off my shoulder. I'm like "Dude, WTF: why don't you go and do something while I finish this?"
he's like: "No, I'm waiting right here until you finish." This is synchronous.
Asynchronous Execution :
When a task is executed asynchronously, you can directly switch to another task before the previous has been completed. One task does not depend on the other.
The boss tells me to do it, and rather than waiting right there for my work, the boss goes off and does other tasks. When I finish my job I simply report to my boss and say: "I'm DONE!" This is Asynchronous Execution.
It's really that simple! Hope it helps.
Real confusion with Asynchronous:
The biggest confusion with this property is the way it works.
Take the example of the given code :
var result = get('https://api.github.com/users/itsmohitt');
If we run the above code, we will get “pending” in the console.But, whats the problem, if called the API and API is returning its data and we are taking that data in variable result.
The problem here is the API calling, whenever we call an API, it will take some time, may be 250ms or 25 seconds, but it will take some.And during that, the next instruction will start executing.To overcome this, you use callback functions.
To run this, we will use callbacks
Now, if you run this, you will get the real result on your console.
Asynchronous property of programming languages provide us the ability to use the system power continuously and doesn't wait for a certain task to perform others. In this way you can call multiple tasks which will take a long time to complete simultaneously.