This course has been updated! We now recommend you take the Complete Intro to Real-Time course.
Transcript from the "Adding Asynquence" Lesson
>> Kyle Simpson: Now your task. I've introduced you a number of times to the asynquence syntax, shown you the asynquence website if you're interested but there should be more than enough information from what I've already shown you and what we've already done for you to turn these setTimeouts. You're not gonna get rid of the setTimeouts, but I want you to turn this from being nested callback setTimeouts into a sequence expressed with asynquence.
[00:00:32] Take lines 5 through 10 and replace them with a chain that calls things through asynquence. Remember, you start out by saying something like ASQ. And I'll also remind you that you're gonna need to make sure you include the asynquence library. You want to say ASQ = require asynquence or whatever you want to call it.
[00:00:57] Now you're gonna do some sort of chain there to express this using a sequence.
>> Kyle Simpson: And again, if you don't like asynquence and you have a different library that you prefer, please feel free to go ahead, queue or Cujo or and in the other ones, feel free to use those.
>> Kyle Simpson: As a little hint to get you started, the first step in your sequence you can pass as a function to a ASQ function. He's going to receive that little done parameter which is what you need to call when you're complete with this step. Inside of there, you're going to do a setTimeout.
>> Kyle Simpson: After 1000 milliseconds. And there on line 8 which I won't write in for you right now, but you're gonna wanna call the done trigger and pass along your random number.
>> Kyle Simpson: Anybody that's got in there. What are we gonna do next? What are we going to do here in line 12?
>> Kyle Simpson: Anyone? We need to do another time out first before we do our rows though that and so let's set ourselves another time out.
>> Kyle Simpson: The other thing is that we need to receive that random number from the previous one. Let's receive that number parameter as the message that's getting passed into us.
[00:04:41] Now we need to call or done trigger for this step after that second timeout runs. And, what are we going to pass along here?
>> Speaker 2: Value?
>> Kyle Simpson: Yeah value but in this case we're going to pass along our completed string which includes both the Hello World characters as well as the number we are passed.
>> Kyle Simpson: Final step. We don't even needed then because it's not going to it is the final step. We can just receive our message. What do we do once we receive our message?
>> Speaker 2: Res.end?
>> Kyle Simpson: Res.end.
>> Kyle Simpson: Everybody see how we did that?
>> Kyle Simpson: Same asynchronicity, same functionality, but now we've stacked it as a vertical [COUGH] sequence of steps [COUGH] that we can reason about.
[00:06:07] And of course we could turn each one of those steps into their own function call so we could have even nicer performing code that isn't all with lots of inline functions. We could have these things factored out into their own functions like I've shown before. Let's save this code and try it just to make sure everything works correctly.
[00:06:26] That's called 5.server let's go back to our code here so I need to kill 4. Let's go back to our code here and say node5.server.js.
>> Kyle Simpson: Come back to our page, and now you see it's waiting for two or three seconds before it prints it out for us.
>> Kyle Simpson: Everybody get it to do that for them?
>> Kyle Simpson: Just as a reminder obviously the set timeouts are intended to be an easy stand in for other sorts of asynchronous tasks like reading from files, writing and reading from a database, etc., etc.
>> Kyle Simpson: All right any questions on putting asynchronicity into this, into the puzzle?