> For the complete documentation index, see [llms.txt](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-3/untitled/basic-concept-of-asynchronous-programming.md).

# Basic Concept of Asynchronous programming

## Concept of Asynchronous Code

{% embed url="<https://youtu.be/wULkuz9rFDc>" %}

> ***A Simple Synchronous Code :***

```dart
void main() {
  
  // Synchronous Code 
  print("test1");
  
  
  
  for (int i = 0; i < 5; i++) {
    print('hello ${i + 1}');
  }
  
  
 
  
  testfunction();
  
  print("test3");
  
  
}


 void testfunction(){
    
   print("test2");
    
  }
```

{% hint style="success" %}
Dart is Single Threaded \
***Note: There is a concept called  "isolates"  in dart which you can use to emulate multithreading***&#x20;
{% endhint %}

> #### Couple of asynchronous processes :

{% hint style="info" %}

* [x] Fetching data over a network
* [x] Writing to a database
* [x] Reading data from a file
  {% endhint %}

### &#x20;Event Loop

{% embed url="<https://youtu.be/CBZevNL4ER0>" %}

### Futures & Callbacks&#x20;

{% embed url="<https://youtu.be/TrqBBoztG0A>" %}

{% hint style="success" %}
"Callbacks" are special types of functions which gets executed when the asynchronous process is done \
\
***Note:  Callbacks are not only used in asynchronous process , they are used in functional programming too .***&#x20;
{% endhint %}

{% tabs %}
{% tab title="First Tab" %}

```dart
void main() {
  // Futures

  print(" Start fetching images");

  getData();

  print("Loading Images");
}

void getData() {
  //callback

  Future.delayed(Duration(seconds: 5), () {
    // Logic
    print(" Image Fetched !!!!!");
  });
}


```

{% endtab %}

{% tab title="Second Tab" %}

```dart
import "dart:html";

void main() {
  // Futures

  print("A");

  var result =
      HttpRequest.getString('https://jsonplaceholder.typicode.com/todos/1');

  print(result);

  print("B");
}


```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Three state of Future " %}
![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MEdhTYvppgDypHNjrd1%2F-MEfeUAIJzadNqI8a_wF%2FScreenshot%202020-08-14%20at%2012.29.51%20PM.png?alt=media\&token=a83a58eb-b75a-4047-9c5c-10d58ae2e7c6)

{% hint style="success" %}
All future start with an uncompleted state . After the asynchronous process is done it either resolves into  "Completed with Data"   or  "Completed with Error"&#x20;
{% endhint %}
{% endtab %}

{% tab title="Future" %}
![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MEffdV4tJjmadL1ckkG%2F-MEffhNBpSqP2zL-s6te%2FScreenshot%202020-08-14%20at%2012.34.09%20PM.png?alt=media\&token=d9ba839a-9f3e-40ce-9a8e-d76901caa59f)
{% endtab %}
{% endtabs %}
