Hey everyone. In the previous post we discussed about Asynchronous Programming. In this post, we will be talking about Futures, Async & Await.
Future:
Some tasks take an unpredictable ( usually, long ) amount of time. In those cases, we use Futures for Asynchronous Programming. But what are Futures? Let me give a real world example:
Suppose you went to a coffee shop. You ordered a cup of coffee and got a receipt . Instead of standing in the queue, you went to your seat with the receipt. You did other things like checking your instagram account. When your order is ready, your order number is called. Then you go and grab your coffee.
This receipt is similar to Future.
At the present moment the Future has nothing but when the task( Asynchronous ) gets completed it becomes something. It can be anything like a String, double, etc. For example:
Future<String>
represents it is a Future that will become a String in sometime.
Async & Await:
We understood Future. If some operation takes unpredictable amount of time, we use Future. Now, what if something is dependent on the value returned by an Asynchronous method ? Let's look into this code:
void main() {
task1();
var task2Data = task2();
task3(task2Data);
}
void task1() {
print('task1 completed');
}
String task2() {
String result;
Duration threeSeconds = Duration(seconds: 3);
Future.delayed(threeSeconds, () {
result = 'task2 data';
});
return result;
}
void task3(String value) {
print('value: $value');
}
Output:
task1 completed
value: null
In this case task3() depends on the value returned by task2(). But since, Future.delayed is an asynchronous method, before result is being initialised result is returned. Hence null is returned instead of a String.
This means, if something is dependent on the value returned by an asynchronous method, it will not get the actual value, but null.
To solve this problem, we use async & await.
By await keyword we mean, wait for the execution to finish and then only proceed to the next step. By this, the asynchronous method will always return an actual value and not null.
Code updated with async & await:
void main() async {
task1();
var task2Data = await task2();
task3(task2Data);
}
void task1() {
print('task1 completed');
}
Future<String> task2() async {
String result;
Duration threeSeconds = Duration(seconds: 3);
await Future.delayed(threeSeconds, () {
result = 'task2 data';
});
return result;
}
void task3(String value) {
print('value: $value');
}
Output:
task1 completed
value: task2 data
Key Takeaways:
Future is used for asynchronous methods.
Futures are kind of receipt. At the moment they are nothing, but after sometime in the future they become something.
When something is dependent on the value returned by an asynchronous method, we use async & await in front of the method.
async keyword is required for writing await keyword.
await keyword can only be used in front of methods that return Future.
By await we mean to wait for the asynchronous method to execute fully and then get the value returned by it. Then move on to the next task.
That's it for this post. Hope you liked it. Would appreciate if you share it with the community, so that others can gain something from it. Please provide feedback in the comments so that I can improve myself. Thanks for your time.
LinkedIn : linkedin.com/in/tanaybanerjeedev