Welcome to Async and Await.
In this learning activity, we'll explore how
async and await is used in mobile programming to allow multiple tasks to
happen at once.
Before beginning our discussion, it's important to understand the difference
between synchronous and asynchronous.
Synchronous processes happen in order.
Each step has to finish before the next step can start.
Asynchronous processes happen all at the same time.
We use synchronous and asynchronous processes every day.
For example, making coffee with your single-serve coffeemaker is a
synchronous process.
First, you need to fill the coffee maker with water.
Next, you pick out your flavor. Then you select the cup size you'd like to make
and press start.
Finally, your coffee maker brews your perfect cup of coffee.
This is a synchronous process because everything has to happen in order.
You can't brew the coffee before you add water or pick the flavor.
You have to follow the process from start to finish.
On the other hand, grilling out is an asynchronous process.
We don't wait for all of the hamburgers to finish cooking before starting to
grill hot dogs.
We grill everything at the same time and remove each item as it's done.
Our grills are usually full with not only hamburgers and hot dogs, but also
corn, chicken, and ribs. All of the cooking happens simultaneously.
Just as you have to follow the process to make a cup of coffee,
most developers also follow a strict process when they're writing code.
It's a fairly straightforward and easy process.
Just like grilling, when you write asynchronous code, you tell the program to
complete multiple tasks at once.
It's incredibly important in the world of mobile technology.
When you write synchronous code, you force every step to wait until the
previous step is complete.
This means nothing can happen if one step is running slowly or gets stuck.
For instance, if you're waiting for a list of items to download on your mobile reader,
you can't scroll through your reading list until the download finishes.
You also can't press any on-screen buttons or delete other messages.
Because everyone expects to multitask in the mobile environment,
synchronous programming just isn't an efficient option.
It can even cause users to believe the app is broken.
The expectation is that everything will work simultaneously,
no matter what other tasks are running.
In our code, we start by getting users once the app has appeared.
Typically, the customer get blocks other actions.
If the user works in an area
with a slow internet connection,
our code prevents them from doing anything else until this web crawl is
complete.
So, how do we change this coding to make this an asynchronous process and keep
our users happy?
We add async and await to our code
so this happens simultaneously.
We start by modifying the first method signature.
Our signature is async task of customer
We add async to this line of code, which tells the compiler that there is an
await keyword somewhere in the remaining code.
Await tells the code to wait until this completes, but it will not block the
thread from performing other actions.
It's important to point out the async is returning a type of task.
Instead of just returning customer now we can return a task of customer.
Even though this is a task, when the action finishes, the compiler
automatically unwraps the task
and returns the customer data, just as it did previously.
The await won't send the response until the task completes.
Once this finishes, the await will send off a message that completes the
remaining requirements in the method.
Async and await adds a bookmark on that line of code, which allows the rest
of the code to run as it normally would.
Today, we've explored how asynchronous programming allows multiple events to
happen on screen at the same time.
You've learned why this is important when programming apps for mobile devices
and saw how this works in code.
You've completed Async and Await.