dart-lang / site-www

Source for Dart website
https://dart.dev
Other
969 stars 702 forks source link

Fix example on 'Asynchronous programming: futures, async, await' tutorial #4573

Open eshfield opened 1 year ago

eshfield commented 1 year ago

Page URL

https://dart.dev/codelabs/async-await#example-introducing-futures

Page source

https://github.com/dart-lang/site-www/tree/main/src/codelabs/async-await.md

Describe the problem

This explanation of the printing order can be confusing:

In the preceding example, even though fetchUserOrder() executes before the print() call on line 8, the console shows the output from line 8(“Fetching user order…”) before the output from fetchUserOrder() (“Large Latte”). This is because fetchUserOrder() delays before it prints “Large Latte”.

The beginner reading this text can think that the key is in that Future.delayed() method but even if we remove the obvious delay we still will have the same behavior and output:

Future<void> fetchUserOrder() {
  return Future(() => print('Large Latte'));
}

void main() {
  fetchUserOrder();
  print('Fetching user order...');
}

We know that the real reason why print('Fetching user order...') executes before print('Large Latte') from the Future is that first one in synchronous (so it runs first) and the second one firstly goes to the Event Queue and then run after all synchronous code is executed.

Expected fix

I don't know how to make it better — maybe it is too much for beginner to met the event loop mechanism from very beginning of that codelab, but for sure the current explanation can lead to misunderstanding of the principles how asynchrony works

Additional context

No response

atsansone commented 1 year ago

Schedule after https://github.com/dart-lang/site-www/issues/4588 completes.