0% found this document useful (0 votes)
87 views5 pages

Day5-3-Asynchronous Programming - 2 Hours - 080220

The document discusses asynchronous programming in Dart. Asynchronous programming allows operations to execute non-blocking and without waiting for other operations to complete. Dart uses futures and the async/await syntax to support asynchronous programming. Futures represent asynchronous operations that will complete at some point in the future. The async keyword marks functions that return futures, while await pauses function execution until the awaited future is complete. Examples demonstrate reading a file asynchronously without blocking other code using futures and async/await.

Uploaded by

noorfatima.okit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views5 pages

Day5-3-Asynchronous Programming - 2 Hours - 080220

The document discusses asynchronous programming in Dart. Asynchronous programming allows operations to execute non-blocking and without waiting for other operations to complete. Dart uses futures and the async/await syntax to support asynchronous programming. Futures represent asynchronous operations that will complete at some point in the future. The async keyword marks functions that return futures, while await pauses function execution until the awaited future is complete. Examples demonstrate reading a file asynchronously without blocking other code using futures and async/await.

Uploaded by

noorfatima.okit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Day5-3-Asynchronous programming - 2 hours https://devnation.notion.site/Day5-3-Asynchronous-programming-2-hou...

Day5-3-Asynchronous
programming - 2 hours
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 -

import 'dart:io'; void main() { print("Enter your favorite car


:"); // prompt for user input String car = stdin.readLineSync();
// this is a synchronous method that reads user input print("The
car is ${car}"); print("End of main"); }

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 a
synchronous method. It means; that the execution of all instructions that
follow the readLineSync() method call will be blocked until

1 of 10 10/11/23, 12:36 AM
Day5-3-Asynchronous programming - 2 hours https://devnation.notion.site/Day5-3-Asynchronous-programming-2-hou...

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 a 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, Peter
2, John
3, Tom
4, Johnson

2 of 10 10/11/23, 12:36 AM
Day5-3-Asynchronous programming - 2 hours https://devnation.notion.site/Day5-3-Asynchronous-programming-2-hou...

4, Johnson

1. 1, Peter
2. 2, John
3. 3, Tom
4. 4, Johnson
Consider the following code.

import "dart:async"; import "dart:io"; void main(){ File file1 =


new File("C:\Users\UsamaSarwar\Desktop\contact.txt"); Future<Str
ing> fs = file1.readAsString(); // returns a future object, it i
s an async method fs.then((data)=>print(data)); // once file is
read, call back method is invoked print("End of main"); }

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 the function asynchronously; we need to add async after
the function name. The syntax is given below:

3 of 10 10/11/23, 12:36 AM
Day5-3-Asynchronous programming - 2 hours https://devnation.notion.site/Day5-3-Asynchronous-programming-2-hou...

Syntax:

func_name() async { //function body }

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 the 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:

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

void hii() async { print("Hii Usama"); } void main() async { awa


it hii(); // Using await keyword print("Task Complete"); }

Output
Hii Usama

4 of 10 10/11/23, 12:36 AM
Day5-3-Asynchronous programming - 2 hours https://devnation.notion.site/Day5-3-Asynchronous-programming-2-hou...

Hii Usama
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.

Copy the above codes and paste it into DartPad below to practice:

Hope you enjoyed

5 of 10 10/11/23, 12:36 AM

You might also like