The I/O cycle. The I/O or the input output cycle is a really important process in the node event loop cycle here. Again, this is the node event or the processing module. We see here a request comes in to the event loop. It checks to see if there's any long run operations and these again are things like bow fetching or database query and so forth. They would send it off to the back where a thread pool that process its formation and it sends that information back through callback function and then sends that off to the user. I/O cycle occurs right here in this area between the event loop and the back thread pool here. Let's go and take a look at an example of this I/O cycle and see how and why it's important in the node event loop. Back in the Visual Studio Code here, make sure we're going to go into the demo folder and I'm going to create a new file in here. If you select a folder here, just click the icon at top that says new file. I'm going to call this just iocycle.js.
First, I want to show you why asynchronous is so important and by first, let's just run a very simple code here to run the asynchronous process. Let's just say here I have a total equal to 0. Very simple operation will let n equals to 1. Then basically you want it to say, I have a function here, call it just say print and this function, I'll just print basically result as you can see. I'll do a total equals and let's just say times 10 and then we print to the console. I'll put a total and I put here the total, very simple like that. Then I'll put the function at the bottom and then up here, so it's much cleaner. I'm just going to invoke the function print. Very straightforward. Let's go and run in the command prompt. Let's go to the command terminal. Make sure again, I'll go into the demo folder and then here is my file, the I/O cycle. To run this in node uses basically, type the keyword node, followed by space and the name of the file or the package or [inaudible] module and ours is called iocycle. You can put the dot js extension if you want. It's optional, you don't have to use it. Just do that as fine too. If I hit return, I get a total of 10 as expected. Nothing unusual here, it's pretty just straightforward JavaScript. What happens when I call this print function inside between lines two and three. We can tell that it's not going to work because, why? Because n was not defined when we called the function. If I save that and go back to the terminal and run the command again, you just press the up arrow key to load that previous command and run there it is. We have the error says, make it a little bit good and says, can I access n because n is not been initialized. Well, the reason is because, of course, if you look at this code here sequentially order from top to bottom, it executes line two. It goes to the print function. It goes in here and there's n, whereas n and this hasn't been declared yet. We declare n after the print function call. That is the error in this case it's a sequential or you run the code synchronously. The fixed is you saw earlier, you put it n above. Sometimes you may not have that luxury. What you do is you can call this function asynchronously and you can do that easily by using one of these functions, called the set timer. I'm going to go here and puts setTimeout. Inside here, you call it a callback function, the print. You don't want to invoke it, you just call that function and it for the time of zero milliseconds now equals zero or one. As a matter, I think the default if you put a zero, anything less than one or be defaulted to one anyway. That should be fine. If I save this now and go back to my terminal, let me clear my console here. So up arrow key two times and run it and you see that now everything comes back to normal as before, even though I declare and define my letter or my variable n after the function call. What happens here is that when you call this timeout function, it triggers the asynchronous call, it sends out this function, this [inaudible] function outside of this loop or this event loop here. At that instance, you have at least guaranteed one iteration through the entire cycle and during that process n has now been declared and set to one. When this whole process, all these code here gets run by the time you end down here, line eight and it goes back to the print function and then it executes the code in here. When you run lines 10 into 11, the n has not been set to one and therefore everything runs very smoothly. That's how asynchronous work. Now, there is another function called setImmediate and I'll put it here. To illustrate this, let's write another function. I'm going to copy this. I'll put it down here. We'll call it print2. Let's make this print1 and then print2. In print2, well, I'll do the multiplication as well. But let's just say in print1, when I call it, after I called the print1, down here I'm going to increase n by one.