What is Asynchronous Function?

Sajid Ahmed
3 min readApr 20, 2020

Asynchronous function is a function that executes asynchronously after it is invoked.

So easy definition but hard to understand,isn’t it?

What does it mean to execute a function asynchronously? That’s the question you are looking for right?

Well, you will have better understanding when I will show you an example below:

Here is a part of dart code but it is also similar to javaScript code.

So,both javascript and dart programmers will be able to understand it.

Suppose,readLocalstorage() is an asyncrhonous method that is used to read interger data which is ‘40’ from device local storage.

And, main() is the method from where readLocalStorage() is invoked.

void main()

{

Var a=20,b=10;

Print(a);

b=readLocalstorage();

Print(b);

}

Output:

20

10

Did you notice any weird things? O.o

Yes,that’s right. The value of b should be 40 after reading from local storage.

Then why it is not changed?

Bacause when main thread reached the line no.3 after executing the line no.1 & line no.2 , it saw that an asynchronous method is called from here.

So , it awakes another thread that is started to execute the readLocalStorage() from 1st line to last line inside of that function.

So,after waking up another thread ,main thread jumps to line no.4 instead of waiting for finishing of readLocalStorage() execution.

For this reason, 4 th line is executed by main thread before that another thread finishes its execution in readLocalStorage() .

So, there are both good sides and bad sides of it.

Good side is that it is making the code execution relatively faster as you can see that the codes that are written below the readLocalstorage()

doesn’t depend on the function call readLocalstorage() .

It is highly used in fetching/downloading data from cloud or local database.

When there are codes of fetching data and load it into UI,the component of UI loading shouldn’t depend on fetching data.

This is why fetching data is executed asynchronously to make the application more faster.

Bad side is that when we need to execute the codes that depend on the fetched value ,it will not work like this.

Then what is the solution??

There are two solutions-

1) Using Promises(async/await).

2) implementing ‘then’ callback function .

Using promises(async/await):

void main()

{

Var a=20,b=10;

Print(a);

val =await readLocalstorage();

b=val;

Print(b);

}

Output:

20

40

So,we want to print the value of b after fetching the value from local storage.

As we know readLocalstorage is a asynchronous function ,it means there is a async keyword with the function name .

So, if we add await keyword in the place from where we are calling readLocalstorage() then the below part of the function call will be executed after fetching a value from that function

Implementing ‘then’ callback function:

void main()

{

Var a=20,b=10;

Print(a);

readLocalstorage().then((val){

b=val;

Print(b);

});

}

Output:

20

40

So,we want to print the value of b after fetching the value from local storage.

This is why Print(b); line is written inside then() .

So,we can understand that the codes that depend on asynchronous function are written inside of then() method .

This then() method is used in javascript and dart language.

In Java,I have seen several types of methods to do this.For example-

onComplete(), onSuccess() etc.

--

--