Tweak page presentation on 'Asynchronous programming: futures, async, await' page #5626
Labels
a.tut.codelab
Relates to codelabs hosted on dart.dev.
e1-hours
Can complete in < 8 hours of normal, not dedicated, work
fix.examples
Adds or changes example
from.page-issue
Reported in a reader-filed concern
p2-medium
Necessary but not urgent concern. Resolve when possible.
st.triage.ltw
Indicates Lead Tech Writer has triaged
Page URL
https://dart.dev/codelabs/async-await/
Page source
https://github.com/dart-lang/site-www/tree/main/./src/content/codelabs/async-await.md
Describe the problem
A minor layout issue
We read at Working with futures: async and await
The examples are now longer side-by-side but one under the other.
Update: Well, it seems this actually depends on what the dynamic layout decides. In the PDF generated from the page, the code blocks are indeed side-by-side.
A minor output issue
Also the output by the synchronous code referred to above, is given as
but in Dart SDK 3.3.0 it is actually
Should a
main
that callsawait
returnvoid
orFuture<T>
?Also
main
is declared aswhich is (at least initially) confusing. Who receives the future returned by
main()
? The compiler is also very happy withand indeed a bit below in Example: Execution within async functions we find:
There should be a word on that, maybe.
In #5624 Eric Ernst writes:
This is extremely informative but I don't know whether it would be too much for the codelab page.
More output to the console in the "do not do this" example
In Incorrectly using an asynchronous function, I suggest instead of just returning
'Large Latte'
in the example's future closure, printing a message to the console too:We go from:
to the more informative:
Extending the code in the simple async example
In Example: Execution within async functions, I suggest extending the
main()
function by:And then ask the reader to flip
doAwait
tofalse
.The output goes from
to
Highlight the dynamic nature of
order
In the example given, the type of
order
actually changes depending on whether youawait
or not. This seems evident but it passed me by on first reading.order
is of typeString
as we recuperate the result of the future.order
is aFuture<String>
as we recuperate the (likely as yet unfinished) future.Future<String>
but we actually return a properString
at the very end of the function. The normal rules of how to declare return values have been suspended, in a sense.More complex example for playing around
As an alternative example for async/await I propose this, which also shows that changing the "context" (what's on the heap), in this case
ingredient
, has effects:Output if one does
await
:Output if one does not
await
:await
is an operatorOne may note that "await" is classed as an "operator" according to https://dart.dev/language/operators
Name-drop the word
continuations
Finally, one should maybe direct the reader to The Wikipedia entry for 'Continuation', which is quite illuminating (to the moderate specialist).
A more verbose for the async/await with try/catch example
This one is a possible extension for Example: async and await with try-catch
Add a diagram for the simple example
The simple example, is slightly modified by printing out more and adding printouts on types:
I drew a sort-of, kind-of, "sequence diagram" of not-fully-fleshed out semantics using the yEd editor. I'm not even sure whether the thread starting/ending at the black circles are really correct, but having that would help enormously (I don't seem to be able to attach the graphml file though):
Simple example, awaiting in
main()
Output:
Simple example, NOT awaiting in
main()
Output:
Expected fix
As described above.
Additional context
No response
I would like to fix this problem.
The text was updated successfully, but these errors were encountered: