Skip to content

Instantly share code, notes, and snippets.

@stegrams
Last active May 6, 2020 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stegrams/e0d3df382087f1c79c7a3b5530ee6435 to your computer and use it in GitHub Desktop.
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.
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