Last active
May 6, 2020 18:15
-
-
Save stegrams/e0d3df382087f1c79c7a3b5530ee6435 to your computer and use it in GitHub Desktop.
If an async returns actual Future the result is NOT a nested Future but a flatten to mere one.
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
String customerOrder = 'Large Latte'; | |
Future<String> delayOrder(Future<String> order) async { | |
// I HAVE TO WAIT THE ARGUMENT FUTURE FIRST OR ELSE | |
// AFTER THE EXPIRATION OF THE INTERNAL FUTURE, | |
// ARGUMENT FUTURE WILL BE EXPIRED TOO. (NOT A BUG) | |
String strOrder = await order; | |
await Future.delayed(Duration(seconds: 1)); | |
// return await order; | |
return strOrder; | |
} | |
void countSeconds(s) { | |
for (var i = 1; i <= s; i++) { | |
Future.delayed(Duration(seconds: i), () => print(i)); | |
} | |
} | |
Future<void> main() async { | |
countSeconds(4); | |
print('Awaiting user order...'); | |
// This IS 4 times delay. | |
var order = await delayOrder( | |
delayOrder( | |
delayOrder( | |
delayOrder( | |
Future.value(customerOrder), | |
), | |
), | |
), | |
); | |
print('Your order is: $order'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment