On Dart DOC site I see this example of asyncronous,
Future<void> printOrderMessage() async {
print('Awaiting user order...');
var order = await fetchUserOrder();
print('Your order is: $order');
}
Future<String> fetchUserOrder() {
return Future.delayed(const Duration(seconds: 4), () => 'Large Latte');
}
Future<void> main() async {
countSeconds(4);
await printOrderMessage();
}
void countSeconds(int s) {
for (var i = 1; i <= s; i++) {
Future.delayed(Duration(seconds: i), () => print(i));
}
}
// output
Awaiting user order...
1 // after 1 second
2 // after 2 second
3 //after 3 second
4 // after 4 second
Your order is: Large Latte // after 4 second
after I changed the code by convert return type of printOrderMessage() and main() to void and remove async and await of main() , is output the same result
printOrderMessage() async {
print('Awaiting user order...');
var order = await fetchUserOrder();
print('Your order is: $order');
}
Future<String> fetchUserOrder() {
return Future.delayed(const Duration(seconds: 4), () => 'Large Latte');
}
main() {
countSeconds(4);
printOrderMessage();
}
void countSeconds(int s) {
for (var i = 1; i <= s; i++) {
Future.delayed(Duration(seconds: i), () => print(i));
}
}
// output
Awaiting user order...
1 // after 1 second
2 // after 2 second
3 //after 3 second
4 // after 4 second
Your order is: Large Latte // after 4 second
Why we ever need to sync and await in main() and return type of Future, if show the same result ?