Skip to content

Instantly share code, notes, and snippets.

@yogithesymbian
Forked from angelabauer/scratch.dart
Created August 16, 2021 23:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogithesymbian/66a6008ee96c5cded9544f82c7d293f7 to your computer and use it in GitHub Desktop.
Save yogithesymbian/66a6008ee96c5cded9544f82c7d293f7 to your computer and use it in GitHub Desktop.
Starting Code for Futures/Async/Await Demo
import 'dart:io';
void main() {
performTasks();
}
void performTasks() {
task1();
task2();
task3();
}
void task1() {
String result = 'task 1 data';
print('Task 1 complete');
}
void task2() {
String result = 'task 2 data';
print('Task 2 complete');
}
void task3() {
String result = 'task 3 data';
print('Task 3 complete');
}
@yogithesymbian
Copy link
Author

import 'dart:io';

void main() {
  performTasks();
}

void performTasks() async {
  task1();
  String? task2data = await task2();
  task3(task2data);
}

void task1() {
  String result = 'task 1 data';
  print('Task 1 complete');
}

Future<String?> task2() async {
  String? result;
  Duration duration = Duration(seconds: 3);
  await Future.delayed(duration, () {
    result = 'task 2 data';
    print('Task 2 complete');
  });
  return result;
}

void task3(String? task2Data) {
  String result = 'task 3 data';
  print('Task 3 complete with $task2Data');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment