Dart Async

 

Dart Async

Dart Async is related to asynchronous programming. It executes the asynchronous operation in a thread. It ensures that the critical functions to be executed until completion. The asynchronous operation is executed, separately the main application thread. In Dart, one operation cannot interrupt the other operation; it means one operation can execute at a time no other part of the program can avert it. Let's understand the following example -

Example -

  1. import 'dart:io';   
  2. void main() {   
  3.    print("Enter your favorite car :");              
  4.      
  5.    // prompt for user input   
  6.    String car = stdin.readLineSync();    
  7.      
  8.    // this is a synchronous method that reads user input   
  9.    print("The car is  ${car}");   
  10.    print("End of main");   
  11. }  

Output

Enter your favorite car :
Renault Duster
The car is Renault Duster
End of main

Explanation:

In the above code, we used the readLineSync(), which is synchronous method. It means; the execution of all instructions that follow the readLineSync() method call will be blocked until the readLineSync() method doesn't complete its execution.

The stdin.readLineSync () executes nothing until it gets the input from the user. It waits for the user input for further execution.

Difference between Asynchronous and Synchronous

Let's understand the difference between synchronous and asynchronous.

In computer science, if we say a particular program is synchronous, that means it waits for an event to execute further. This approach is driven with a demerit, if a part of the code takes much time to execute, the succeeding blocks through an unrelated block will be blocked from executing.

This is the main problem of the synchronous approach. A part of the program may require executing before the current part, but the synchronous approach doesn't allow it.

This is not suitable for the webservers, where request must be independent of the others. It means, the webserver does not wait to finish the execution of the current request, it responds to the request from the other users.

The web server should accept the request from the other user before executing the previous requests.

This approach is called asynchronous programming. The asynchronous programming generally focuses on no waiting or non-blocking programming model. The dart: async is facilitated to implement the asynchronous programming block in a Dart script.

Example -

We create a file with the few names and save this file as names.txt and write a program to read this file without delaying the other part of the code.

  1. 1, Peter  
  2. 2, John  
  3. 3, Tom  
  4. 4, Johnson  

Consider the following code.

  1. import "dart:async";   
  2. import "dart:io";    
  3.   
  4. void main(){   
  5.    File file1 = new File("C:\Users\DEVANSH SHARMA\Desktop\contact.txt");   
  6.    Future<String> fs = file1.readAsString();    
  7.     
  8.    // returns a future object, it is an async method   
  9.    fs.then((data)=>print(data));    
  10.      
  11.    // once file is read, call back method is invoked    
  12.    print("End of main");    
  13.    
  14. }  

Output

End of main
1, Peter
2, John
3, Tom
4,  Johnson

Dart Future

The Dart Future is defined as getting a result sometime in the future. The Future object uses to facilitate asynchronous programming. Future objects are a tool to denote values returned by an expression whose execution will complete at a later point in time (In Future). In order to work with the future, we can use either async and await or the Future API.

Dart async and await

The async and await keywords are allowed to implement asynchronous programming without using the Future API. The async keyword is necessary to run function asynchronously; we need to add async after the function name. The syntax is given below:

Syntax:

  1. func_name() async {  
  2.    //function body  
  3. }  

When an async function is invoked, the Future object instantly returns and that indicates the async function will execute later. Once the body of the async function is executed, the function call returned the Future object. The function call will be completed with its result.

Dart await Keyword

The await keyword is also used to execute function asynchronously. It suspends the currently running function until the result is ready. When it returns the result, then it continues on to the next line of code. The await keyword can only be used with async functions. The syntax is given below.

Syntax:

  1. await e;  

Here, e is an asynchronous expression, and it is expected to evaluate to a Future. The await expression evaluates e, and then suspends the currently running function until the result is ready.

Let's understand the following example -

Example - 1

  1. void hii() async {  
  2.          print("Hii Javatpoint");  
  3.   
  4. }  
  5.   
  6. void main() async {  
  7.          await hii();            // Using await keyword  
  8.          print("Task Complete");  
  9.   
  10. }  

Output

Hii JavaTpoint
Task Complete

Explanation:

Here, we have declared the main() function asynchronous using the async keyword because we call the hii() method asynchronously. Then, we used await modifier to call hii() that executed asynchronously.

Note - The caller function and called function must use the async keyword to use await.

Comments