The async function is useful when you need to return a value from your coroutine. In this video, learn about the use of the Deferred type as well, as the await() function.
- [Instructor] The Async function is useful when you need to return a value from your coroutine. So we're going to implement the methods here in the coroutine's Async test class in order to try it out. We're going to first start by using the Async method the same way that we would use the launch method. So for that we're going to grab some code from the coroutine's launch class, so let's switch over there now. And we'll grab the code from this first test. Go back to our test here on line nine and paste that in.
Let's accept all of our imports. And the only thing we're going to change is here on line 14. Instead of using the launch function we're going to use Async. So let's go ahead and run the test. So if we look at our results we're getting starting, inside and stopping. The same thing we had before just like when we were running launch, except for now we're taking advantage of the Async call. So now let's look at a better usage of the Async call.
For that we're going to implement the usingAsyncWithDeferred test method. So let's close this down. We're going to copy this same code once again to make it easier on ourselves. And paste that here on line 27. Now we're going to see two new things. First the use of the result being returned, and then a call to the await method. So here on line 29 we're going to create a variable for ourselves that we're going to result. And the type of it is going to be a Deferred integer.
Now notice that we receive an error message. So if we just kind of mouse over that it's letting us know that the type inference is off because we're not actually returning an integer yet. So right underneath where we have our println statement, we're going to return the number 42. So now that error has gone away. And you can basically think of Deferred as a future or promise of a value. And what we want to do is get that future value and print it out. So let's do that down here on line 32.
We're going to make a call to println and we're just going to print out the result. Now if you notice the type of the result is a Deferred, so that's not really useful to us because we want to get at the actual integer value, and in order to do that we're going to have to make a call to the await function. Now notice that await is a suspending function so it can only be called from another coroutine or a suspending function itself.
And so that's why the next step is to wrap this inside of another coroutine. So we're going to take all of this code here and we're going to create a new coroutine. And for this one we can use launch. And once again we'll use the common pool. So we're going to put all this code inside of here. And now that error goes away. This is really important so whenever you're working with suspending functions they always have to be called from another suspending function or a coroutine.
So let's run this test to see if we can get our result. So we getting starting, inside, the result which is our number 42, and then stopping. So this is how we can use Async in order to get a result in the future. Now another common process in application development is needing to execute two asynchronous processes and then combine the results together. This is trivial with coroutines. We're going to copy the same contents from above so we're going to copy the same thing we have here inside of our usingAsyncWithDeferred function And we're going to paste that down on line 43 where we use Async twice.
Now let's just make a few small changes. So here on line 48 we're going to say inside first, and then we'll copy this. And right underneath we're going to paste it again. Let's give this variable a new name, we'll call it result2, the one here on line 52. And then on line 54 let's say inside second. And instead of returning the number 42, we're going to return the number 8. This way we can do some really simple math.
And then down on line 58 instead of calling the result we're going to do the sum of the first one and the second coroutine. So we'll call await on both. We have our Thread.sleep and we still have our stopping println. So let's go ahead and run this together and see how we can combine two coroutines asynchronously. So we get starting, inside first, inside second, and the sum of 50, and then finally stopping.
So we correctly get the result of adding those two values together even though they took place asynchronously. This is because the await function allows the printing of this value to be suspended until both of those asynchronous coroutines are completed.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections