-
Notifications
You must be signed in to change notification settings - Fork 702
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
async await codelab #1659
Changes from 3 commits
7cf3303
cd75e7f
33a671b
3934ee6
b34a5d7
b587b7f
7396cc6
0e169e0
d2bafa7
846944c
60374bf
48f7c3a
0fd4ed2
4417dd9
11427cf
a63570b
38b3d5b
5f9d525
c0c98ae
5ff8fb1
d18e711
71b621c
0c92c5c
9d136d3
3120d87
123f576
f67ffc4
5e5ec20
125f9a3
4ee31a2
64dc921
da8e3e3
95a7ab7
c151e1d
86367c6
d7b72c0
0e6a5a9
cc371e2
60a0d36
f48f067
2c8c32f
9621eb3
e0aa7b8
f672a1a
b2a44ce
cdda574
37b6c04
6624930
fb19d9c
f48d9c3
218c301
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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? | ||
<!-- [//]: 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
|
||
|
@@ -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 --> | ||
|
||
|
@@ -430,11 +429,7 @@ width="100%" > | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that this exercise doesn't belong under Handling errors. Changing |
||
It's time to practice what you've learned in one final exercise. | ||
To simulate asynchronous operations, this exercise provides the asynchronous | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | | ||
|
@@ -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()` | ||
|
@@ -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()`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returned by calling -> produced (eventually) by (or something else that acknowledges that logoutUser doesn't return a string value) |
||
|
||
|
@@ -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? | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete "before running it"