Exploring Async Iteration
00:00 With all the theory now covered, you are ready to introduce async iteration into your code example. And again, feel free to download the code instead of typing everything from scratch.
00:11 That should make following along just a little bit easier.
00:15 So in your example, you will start from the same three tasks. So remember they were starting up the laptop, making coffee, and opening a parcel, but each of those three tasks now has subtasks.
00:27 And importantly, these subtasks need to be performed in a fixed order. So what does that mean? Here are the three tasks. So starting up a laptop still takes 10 minutes, but you now have two subtasks.
00:39 First, you have to click the on button and you have to wait for two minutes, and then you enter your password and wait for eight minutes. So in total, that is still 10 minutes, but there are two subtasks to the task starting up your laptop. Making coffee still takes three minutes, but you first have to boil water for two minutes.
00:58 And only then can you add coffee, milk, and sugar and let it brew for a minute. Opening your parcel still takes three minutes, but now you firstly have to get the scissors, then cut open the packaging, and then throw away the packaging when you are done.
01:15 Now, what is important here is that these subtasks within each task have to happen in this particular order. For example, starting up your laptop, you cannot enter your password before you clicked the on button.
01:29 And for making coffee, you cannot add coffee, milk, and sugar to the water and let it brew if the water hasn’t boiled first. And for opening your parcel, you cannot throw away the packaging before you have actually cut away from the object you ordered, and you cannot cut it away before you got the scissors.
01:50 So those are the three tasks and the subtasks. So each of these three tasks is going to be modeled as an async iterator iterating over the tasks subtasks in that fixed order.
02:03 Now, at the end of each iteration, within each iterator, the event loop will check for multitask opportunities, or if you want to read that differently, at the end of each subtask, within each task, the event loop will check for multitask opportunities.
02:21 Now, the iteration within each iterator still happens sequentially. That is important. So the subtasks will always get performed in a fixed order, and multitasking then happens between the subtasks of those three non-blocking iterators.
02:38 So your mission, should you choose to accept it, is to build these iterators in the next lesson.
Become a Member to join the conversation.
