Skip to content

Instantly share code, notes, and snippets.

@PiN73
Created July 4, 2019 12:14
Show Gist options
  • Save PiN73/69ec3785d5142c522358484a8defe755 to your computer and use it in GitHub Desktop.
Save PiN73/69ec3785d5142c522358484a8defe755 to your computer and use it in GitHub Desktop.
await two Futures in parallel
void main() async {
final square3Future = square(3);
final square4Future = square(4);
final square3 = await square3Future;
final square4 = await square4Future;
int sum = square3 + square4;
print('3^2 + 4^2 = $sum');
}
Future<int> square(int val) async {
print('${DateTime.now()} started calculating $val^2');
await Future.delayed(Duration(seconds: 1));
int result = val * val;
print('${DateTime.now()} finished calculating $val^2');
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment