Created
September 24, 2019 12:32
-
-
Save Wassmd/5296059e4b363e88ffe3d779a86551d7 to your computer and use it in GitHub Desktop.
Async programming with Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
void main() { | |
performTasks(); | |
} | |
void performTasks() async { | |
task1(); | |
String output = await task2(); | |
task3(output); | |
} | |
void task1() { | |
String result = "Task1 performed"; | |
DateTime dateTime = DateTime.now(); | |
print('$dateTime $result'); | |
} | |
Future task2() async { | |
Duration threeSeconds = Duration(seconds: 3); | |
//sleep(duration); | |
String result; | |
await Future.delayed(threeSeconds, () { | |
result = "Task2 result"; | |
DateTime dateTime = DateTime.now(); | |
print('$dateTime Task2 is complete and result is prepared'); | |
}); | |
return result; | |
} | |
void task3(String resultOfTask2) { | |
String result = "Task3 performed"; | |
DateTime dateTime = DateTime.now(); | |
print('$dateTime $result with $resultOfTask2'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment