Instructor>Hello and welcome. In this lesson, you will learn about the foundations of asynchronous programming. You'll learn what the term actually means and why it's so important in the user interface framework of Android. So the reason we have asynchronous programming is because certain functions in programming take a long time to execute, such as the call, such as networking, such as an HTTP request, loading media files, calling a server, calling an API, or calling a heavy calculation. All of these functions take a while to do. So programmers came up with concept of asynchronous programming, whereby you don't have to run all of your functions sequentially or in a synchronous fashion. Instead, you can run it in an asynchronous fashion to do parallel work or run functions and concurrency. So all of those situations where you have a long call to a function, it's critical that you want to avoid all that poor performance that can cause your app to be unresponsive or slow, or even to crash. So we have this method called asynchronous programming where we can send your logic into an alternative branch or an alternative thread while the main thread continues with the heavy load. So you can create asynchronous functions to do two different things or multiple things. And if it's doing one thing successfully, then you can stay on the main thread. But if something goes wrong, you can branch out to another thread. So you are simply doing asynchronous programming whenever you call a function that's been set up in an asynchronous way, so that when the program starts down a thread, it might just sit there for awhile and wait for you to return a value. Like I'll click on this and it's waiting for this value here to be returned, but that value might not get returned immediately. But at some point in the future, it will get returned, and that will be handled in an asynchronous way. And if you're performing a network request or you're querying a database, there's almost certainly going to be a delay in your functionality. And when there's delay and you're trying something in a synchronous fashion, then you're gonna have big problems. So it's very important to write your code in a way that anticipates the delay or the block, and you cut it in a way so that your logic runs in a blocking fashion so it blocks the delay and chooses a different route or a different thread. So if the main thread is waiting to receive a response from a network request, in asynchronous programming, it would be blocked until it's able to take care of those responsibilities. And meanwhile, the program can go on to do something else depending on whether there's success or failure. And it does that asynchronously. So there's several ways to handle asynchronous programming. One of the older ways is to use a callback pattern, whereby the function returns our calls back a certain value. Another way the Kotlin does it is to use coroutines, which was created by Kotlin to handle asynchronous programming in a very efficient way, called coroutines. And we use these so that the flow of your Kotlin program can move in an efficient way throughout the three entities of a program flow. And that program flow always has a thing that will produce data. And then there's a thing that will modify the data. And then there's a thing that will use the data, and that's normally called a data consumer. So I'm gonna produce data by making this click, and then there's a modification. And then we consume the data which is here. And we do that through user interaction. So we do asynchronous programming, and especially, in Kotlin, we do coroutines for two reasons. One is, at some point, we're gonna have a long running task that's doing heavy computations and it's blocking the main thread. So we have to asynchronously go around it. And then the other reason is for safety reasons where we have to suspend a function until we can make it successful. Now, every Kotlin program begins with a main thread or a main activity. And it will stay on that main thread through all of your UI task until you code something else. So you have to coordinate your task with the user interaction. And you could do that through asynchronous programming and coordinate through coroutines. So all of this goes to user experience and enhancing the user experience, and improving the user interaction, because asynchronous programming allows the user to do things in the application like click on a button while another process is running in the background and producing a different kind of result. And all of this enhances the user experience. So that concludes our lesson on the foundations of asynchronous programming. Thanks for watching. Stay tuned for the next lesson. So let's go.