Let's take a look at how we can use tasks to run our futures.
So inside of Main, we're going to say let task equals task spawn.
And now we're going to insert an async code block.
And then inside this code block, we're going to say let result equals.
And we want to call our read file function and we'll call, read text.
And now we want to wait for the future result to be returned.
And I went ahead and created a text file and I just gave it some contents that say file to read from.
So something super simple.
So now we can come back here and we can rematch on.
Result because we are returning a result from our function.
So in the event that everything is okay, we want to return.
And print out the contents of what was returned to us.
And then if we get an error, obviously we want to print out what our heir was.
So we're reading from file.
And now we want to print out our air.
So.
Spawn takes a future and starts running.
Dawn a task.
And we can see that it returns a joint handle, just like we saw in our concurrency section.
So a task is very similar to a thread with some very minor differences.
The task will be scheduled by the program, and if the task has to wait, the program is responsible
for waking it back up.
We also see that we have an async block.
Inside of our spawn, and it is necessary to have an async blank block in order to call async functions.
And then just like in threads, we can call a blocking function to allow for the computation to finish.
So let us do that.
So we will say print line.
Task has started and now we want to call our block on.
So this is our blocking function on task.
Which will make which will allow it to finish.
And now we will say stopped task.
And now if we run this, we accept we expect it to say task is started and then we expect the contents
to be printed out and then we expect stoped task to be printed out after.
And we see task has started file to read from which was the contents inside of our file and then we
see stop task.
So task are one of the core abstractions in async.
Like threads, they provide some practical functionality over the raw concept.
Tasks have a number of desirable properties such as being allocated in one single allocation.
They have backchannel which allows them to propagate results and errors to the spawning task through
the joint handle.
They also carry useful metadata for debugging and they support task local storage.
So all in all we can conclude that the task type.
And our async library is very similar to a thread which is great for us since we are already familiar
with threads and as you can tell, kind of by the structure of our program here.
It has a very, very, very similar feel to what we've already seen in the concurrency section.
So in order for us to really understand asynchronous programming and how it works and just get an overall
better feel for it, let's create a client server chat application.
Make it asynchronous.
So that way we can practice using async code and rest and get a better understanding of how asynchronous
programming works.
And we will begin doing this in the next lecture.