Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async await codelab #1659

Merged
merged 51 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7cf3303
Add WIP async await codelab
legalcodes Jun 4, 2019
cd75e7f
Limit lines to 80 chars
legalcodes Jun 4, 2019
33a671b
Round 1 of revisions:
legalcodes Jun 5, 2019
3934ee6
Add more updates per language comments/suggestions review feedback
legalcodes Jun 5, 2019
b34a5d7
Further small updates addressing review comments.
legalcodes Jun 5, 2019
b587b7f
Configure site.alert templates and update futures codelab w/ templates
legalcodes Jun 6, 2019
7396cc6
Add "Practicing" section iframe; slight restructure for "Errors" section
legalcodes Jun 6, 2019
0e169e0
Added dartpad examples
legalcodes Jun 10, 2019
d2bafa7
Remove extra title
legalcodes Jun 11, 2019
846944c
Remove section head and use of "future object"
legalcodes Jun 11, 2019
60374bf
Remove typos
legalcodes Jun 11, 2019
48f7c3a
Merge branch 'master' of https://github.com/dart-lang/site-www into f…
legalcodes Jun 11, 2019
0fd4ed2
Punctuate all bulleted list items.
legalcodes Jun 12, 2019
4417dd9
Allow check-code to fail for dart dev builds
legalcodes Jun 12, 2019
11427cf
Correct mistaken exclusion of check-code from builds
legalcodes Jun 12, 2019
a63570b
Allow failure for check-code.sh
legalcodes Jun 12, 2019
38b3d5b
Merge branch 'master' of https://github.com/dart-lang/site-www into f…
legalcodes Jun 12, 2019
5f9d525
Allow failure for both check-code and analyze-and-test-examples
legalcodes Jun 12, 2019
c0c98ae
Updates per ux and tw review
legalcodes Jun 19, 2019
5ff8fb1
More updates per review from @sfshaza2
legalcodes Jun 19, 2019
d18e711
Updates per latest sfshaza2 comments
legalcodes Jul 9, 2019
71b621c
Merge branch 'master' of https://github.com/dart-lang/site-www into f…
legalcodes Jul 9, 2019
0c92c5c
Incorporate first part of changes from UX round 2
legalcodes Jul 11, 2019
9d136d3
Merge branch 'master' of https://github.com/dart-lang/site-www into f…
legalcodes Jul 15, 2019
3120d87
Correct typos, turn on latest DartPad UI, and misc small fxies
legalcodes Jul 16, 2019
123f576
Update descriptions for examples
legalcodes Jul 16, 2019
f67ffc4
Add updates from observing test user
legalcodes Jul 18, 2019
5e5ec20
Minor update for example missing a border, extra sentence for DartPad
legalcodes Jul 19, 2019
125f9a3
Re-organize introduction per latest feedback:
legalcodes Jul 20, 2019
4ee31a2
Remove "!" characters from "Putting it all together" section
legalcodes Jul 24, 2019
64dc921
Adding changes from latest review
legalcodes Jul 30, 2019
da8e3e3
Refactored sync / async comparison section into 2 columns
legalcodes Jul 31, 2019
95a7ab7
Updated instructions before each example
legalcodes Jul 31, 2019
c151e1d
Add changes before switching markup back to single-column view
legalcodes Aug 1, 2019
86367c6
Completed markup re-write
legalcodes Aug 2, 2019
d7b72c0
Finished updates from latest review comments
legalcodes Aug 2, 2019
0e6a5a9
End with new lines for .travis.yml and .firebaserc
legalcodes Aug 2, 2019
cc371e2
Remove dartpad includes file, update nav per review comments
legalcodes Aug 2, 2019
60a0d36
Update codelab title
legalcodes Aug 2, 2019
f48f067
Incorporate suggestions from latest review
legalcodes Aug 2, 2019
2c8c32f
Enforce 80 char line limit
legalcodes Aug 2, 2019
9621eb3
Small changes from latest review
legalcodes Aug 5, 2019
e0aa7b8
Updates from latest review
legalcodes Aug 6, 2019
f672a1a
Test omitting refresh-code-excerpts.sh - failing built
legalcodes Aug 6, 2019
b2a44ce
Remove test fix for build
legalcodes Aug 6, 2019
cdda574
Some changes based on recent review
legalcodes Aug 6, 2019
37b6c04
Incorporate latest review items
legalcodes Aug 6, 2019
6624930
Remove 'before running it'
legalcodes Aug 6, 2019
fb19d9c
Exercise applies to "Working with futures"
legalcodes Aug 7, 2019
f48d9c3
More updates - latest review
legalcodes Aug 7, 2019
218c301
Add survey at the end of the codelab
legalcodes Aug 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions src/codelabs/async-await/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ and the `async` and `await` keywords.
### Example: Incorrectly using an asynchronous function
The following example shows the wrong way to use an asynchronous function
(`getUserOrder()`). Later you'll fix the example using `async` and `await`.
Before running this example, try to spot the issue -- can you guess what the
Before running this example, try to spot the issue -- what do you think the
output will be?

[//]: https://gist.github.com/5c8c7716b6b4284842f15fe079f61e47
Expand Down Expand Up @@ -368,7 +368,7 @@ Implement an `async` function `reportLogins()` so that it does the following:
</iframe>

{{ site.alert.info }}
If your tests pass, you can ignore [info-level messages.](/guides/language/analysis-options#customizing-analysis-rules)
If your code passes the tests, you can ignore [info-level messages.](/guides/language/analysis-options#customizing-analysis-rules)
{{ site.alert.end }}

## Handling errors
Expand All @@ -387,7 +387,7 @@ the same way you would in synchronous code.

### Example: async and await with try-catch
Run the following example to see how to handle an error from an
asynchronous function. Can you guess what the output will be before running it?
asynchronous function. What do you think the output will be before running it?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete "before running it"

<!-- [//]: https://gist.github.com/25ade03f0632878a9169209e3cd7bef2 -->
<iframe
src="https://dartpad.dartlang.org/experimental/embed-new-dart.html?id=25ade03f0632878a9169209e3cd7bef2"
kwalrath marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -399,23 +399,22 @@ asynchronous function. Can you guess what the output will be before running it?

### Exercise: Practice handling errors

The following exercise provides practice handling errors with asynchronous code
The following exercise provides practice handling errors with asynchronous code,
using the approach described in the previous section. To simulate asynchronous
operations, your code will call the following function, which is provided for you:

|------------------+-----------------------------------+-------------|
| Function | Type signature | Description |
|------------------|-----------------------------------|-------------|
| getNewUsername() | `Future<String> getNewUsername()` | Returns the username from a user-initiated request for update|
| getNewUsername() | `Future<String> getNewUsername()` | Returns the new username that you can use to replace an old one.|
{:.table .table-striped}

Use `async` and `await` to do the following:
* Implement an asynchronous `changeUsername()` function that calls the provided
asynchronous function `getNewUsername()` and returns its result.
Use `async` and `await` to implement an asynchronous `changeUsername()` function
that does the following:
* Calls the provided asynchronous function `getNewUsername()` and returns its result.
* Example return value from `changeUsername()`: `"jane_smith_92"`
* Because `getNewUsername()` can encounter errors, the `changeUsername()`
function must __catch and return any errors__ and it must __stringify the error before returning it__.
* You can use the [toString()]({{site.dart_api}}/stable/dart-core/ArgumentError/toString.html) method to stringify both [Exceptions]({{site.dart_api}}/stable/dart-core/Exception-class.html) and [Errors.]({{site.dart_api}}/stable/dart-core/Error-class.html)
* Catches any error that occurs and returns the string value of the error.
* You can use the [toString()]({{site.dart_api}}/stable/dart-core/ArgumentError/toString.html) method to stringify both [Exceptions]({{site.dart_api}}/stable/dart-core/Exception-class.html) and [Errors.]({{site.dart_api}}/stable/dart-core/Error-class.html)

<!-- [//]: https://gist.github.com/858f71f0ad0e70051999bcafa41806a3 -->

Expand All @@ -430,11 +429,7 @@ width="100%" >

Copy link
Contributor

@galeyang galeyang Aug 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(my quote is weird --- I was referring to the heading of "Exercise: Putting it all together)
In ToC, it's under "Handling errors". I guess it should be move a level up? Btw, I like that latest ToC is more concise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kwalrath I could be wrong, but I think ToC level for exercises and examples may be a point of disagreement here. If it is, I'll leave it to you two.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm out next Monday. Feel free to ignore this one if other content are ready to go. It's a minor point. We can revisit later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this exercise doesn't belong under Handling errors. Changing ### to ## makes sense here.

It's time to practice what you've learned in one final exercise.
To simulate asynchronous operations, this exercise provides the asynchronous
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this sentence and the next one, or combine them somehow with the next paragraph. (As is, it's too repetitive.)

functions `getUsername()` and `logoutUser()`. You don’t need to implement these
provided functions. You don't need to implement `main()`.

To simulate asynchronous operations, your code will call the following
functions, which are provided for you:
functions `getUsername()` and `logoutUser()`:

|------------------+-----------------------------------+-------------|
| Function | Type signature | Description |
Expand All @@ -447,7 +442,6 @@ Write the following:

#### Part 1: `addHello()`
* Write a function `addHello()` that takes a single String argument
<!-- try to avoid should -->
* `addHello()` returns its String argument surrounded by the text ‘Hello \<string\>'

#### Part 2: `greetUser()`
Expand All @@ -457,15 +451,13 @@ function `getUsername()`
* `greetUser()` creates a greeting for the user by calling `addHello()`,
passing it the username, and returning the result.
* For example, if the username is "Jenny", `greetUser()` should create and
return the greeting "Hello Jenny"
return the following: "Hello Jenny"

#### Part 3: `sayGoodbye()`
<!-- pull out descriptions of -->
* Write a function `sayGoodbye()` that does the following:
* `sayGoodbye()` takes no arguments.
* `sayGoodbye()` catches any errors.
* `sayGoodbye()` calls the provided asynchronous function `logoutUser()`.
<!-- italicize <result> -->
* Takes no arguments.
* Catches any errors.
* Calls the provided asynchronous function `logoutUser()`.
* If `logoutUser()` succeeds, `sayGoodbye()` returns the string "\<result\>
Thanks, see you next time" where \<result\> is the String value returned by calling `logoutUser()`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returned by calling -> produced (eventually) by logoutUser().

(or something else that acknowledges that logoutUser doesn't return a string value)


Expand All @@ -478,18 +470,21 @@ Thanks, see you next time" where \<result\> is the String value returned by call
</iframe>

{{ site.alert.info }}
You might have noticed that the functions in the exercises don't have return types. That's because Dart can infer the return type for you. Omitting return types is fine when you're prototyping, but when you write production code, we recommend that you specify the return type.
Dart can [infer the return type](https://dart.dev/guides/language/sound-dart#type-inference) for you.
You might have noticed that the functions in the exercises don't have return types. That's because Dart can
[infer the return type](https://dart.dev/guides/language/sound-dart#type-inference)
for you. Omitting return types is fine when you're prototyping, but when you write production code, we recommend that you specify the return type.
{{ site.alert.end }}

## What's next?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a brief intro paragraph here.

* [Asynchronous programming: futures & async-await](/tutorials/language/futures) tutorial
* [Dart's type system](/guides/language/sound-dart) (Includes more examples of type signatures with futures).
* [Preferring signatures in function type annotations](/guides/language/effective-dart/design#prefer-signatures-in-function-type-annotations)
* [Effective Dart](/guides/language/effective-dart)
* Try [other Dart codelabs](/codelabs).
Congratulations, you've finished the codelab! If you'd like to learn more, here
are some suggestions for other topics to explore:

* Play with [DartPad.]({{site.dartpad}})
* Try another codelab or a tutorial:
* [Dart codelabs](/codelabs).
* [Asynchronous programming: futures & async-await](/tutorials/language/futures) tutorial.
* Read about [Dart's type system](/guides/language/sound-dart).

[future class]: {{site.dart_api}}/stable/dart-async/Future-class.html
[style guide]: /guides/language/effective-dart/style
Expand Down
6 changes: 3 additions & 3 deletions tool/check-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ Error: some code excerpts need to be refreshed.
Rerun '$rootDir/tool/refresh-code-excerpts.sh' locally.
"

# travis_fold start refresh_code_excerpts
# (set -x; $rootdir/tool/refresh-code-excerpts.sh) || (printf "$errormessage" && exit 1)
# travis_fold end refresh_code_excerpts
travis_fold start refresh_code_excerpts
(set -x; $rootDir/tool/refresh-code-excerpts.sh) || (printf "$errorMessage" && exit 1)
travis_fold end refresh_code_excerpts