Check out a free preview of the full Real-Time Web with Node.js course:
The "Simulating Asyncronicity" Lesson is part of the full, Real-Time Web with Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle tasks the audience with adding a couple setTimeout statements to simulate an asynchronous behavior.

Get Unlimited Access Now

Transcript from the "Simulating Asyncronicity" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Kyle: By a show of hands, how many of you feel like you've written node code now?
>> Kyle: If anybody doesn't raise their hand, I feel like I'm failing. Okay, yeah. Well, there's more to go. We're not even halfway through. We're just about to start five and the plan calls for us to go through eleven.

[00:00:24] So there's more to go. There's plenty more fun stuff for us to learn about. So speaking of that let's take our 4.Serve.JS and save as to5.server.Js. All right so what I want to do is I want to rather than show you how to do this. I want to tell you what I want and have you attempt it.

[00:00:51] And then again I'll kind of talk through how I would approach it. So the first thing that I want you guys to do and I'll give you maybe a minute and a half to do this. This is not hard, but I want you to set up a couple of timeouts.

[00:01:03] You'll use the callback methodology. We're going to simulate a little bit of asynchronicity. And what I want you to do is in the first time out, you can make these take 500 milliseconds apiece or a second apiece, however long you want to do it. But in the first timeout after the 500 milliseconds or 1,000 passes, I want you to generate a random number and store that off in a variable.

[00:01:29] And then I want you to wait another 500 or 1,000 milliseconds and I want you to print a string, I mean, put together a string, the Hello World plus the random number. And then in the third delay after another 500 or 1,000 milliseconds, I want you to write that out to the response stream.

[00:01:49] So it's a three step process. Wait and then generate the random number, wait and then add it to the string, wait and then send that out to the browser. Okay? And so, you could start by doing that with a couple of set timeouts. But I will let you try your hand at it, and you'll do that right there, basically, on line 5.

[00:02:16] Then turn line five into [COUGH] some code to do that.
>> Kyle: All right. So, you should be at least partially along in this what we would wanna do is first set up a setTimeout with the function that's gonna run after let's say 500 milliseconds or so. And this setTimeout inside of that, we want to generate a random number.

[00:03:45] So I'm gonna say, num = Math.random. And then I want to wait another
>> Kyle: Set of time. Let's say 500 milliseconds.
>> Kyle: Let me verify just make sure whether I did two or three. I can't remember if I said two or three in the other one.
>> Kyle: I know what it was, okay.

[00:04:23] So [COUGH]
>> Kyle: This is where we're going to do our,
>> Kyle: Res.end.Hello World and we're going to use the number that we generated in our previous step. And I can change those back to 1000. So I misstated earlier when I said doing three delays. In this case we only need to do the two delays.

[00:04:55] The first delay to generate the random number. Second delay to send out the response. Did everybody gets something roughly approximate to that? Seeing several nodding heads, okay. Wave wildly at me. If you feel like we're getting off track from what you've been doing.