This course has been updated! We now recommend you take the Complete Intro to Real-Time course.
Transcript from the "Kickstarter Survey" Lesson
>> Kyle Simpson: I'm gonna take a diversion from this code for just a moment and I'm gonna go over to one of my repos.
>> Kyle Simpson: Happens to be the one for the book. There's a site that I built for my Kickstarter backers. It's what I use to survey the Kickstarter backers and get their information, so I know where to send their t-shirts and stuff.
[00:00:25] [COUGH] And so I have the code for that whole survey site built up here. And by the way, if you are interested at all in example code of how to perhaps implement middle-end based on Node.js. I'm using some of those middle end concepts here. So I have a server JS file, which is kind of my main entry point.
[00:00:48] And, inside of my server file I didn't do a lot of work to organize this out into modules, but obviously, a lot of this code we. Or [COUGH] organize in the modules. [COUGH] But there's, I wanna take you to one particular place, the profile.get path which is called do get profiles.
[00:01:07] Let's go to the function, do get profile. And I wanna show you cuz I'm dog fooding all my own stuff in terms of a sequence here. Let me make that a lot bigger. That's way too small. This is the function that gets called whenever I need to retrieve the profile for my logged in user.
[00:01:27] So the first thing that you notice, I'm getting a request in a response stream. We'll understand that when we get into a little bit further into node. But I have a request in a response stream for the incoming request. The first thing I wanna do is collect any post data that's on the request stream.
[00:01:41] So, I call that function who is of course going to generate a sequence and return it back to me. And then .val, remember .val is kind of like our synchronous step. These are just synchronous transformations steps. So I wanna process the JSON post data, that's a function that will take the data from the previous step and do some processing on it and pass along the process data.
[00:02:05] I receive the session data and off of the session data I get the session ID. I pass the session ID along which means now we need to do a validate of my session. Now that's an asynchronous task, so I don't do .val here. I do a .SCQ which means fire off another function who's gonna give us back a sequence.
[00:02:24] So validate session gets a session ID into him. He returns us back a promised sequence and whenever he completes, then we move on to the next thing. He'll send us back a session ID if we validated. Now if we didn't validate, he'd send us back an error which would propagate into our sequence and we would respond back with an error.
[00:02:43] But if we got a valid session ID back then we know everything's fine with the session ID. And we pull out the user ID from our sessions table from our sessions hash. Now we go off and retrieve their user profile based upon their user ID. And that's an asynchronous task, it's gonna make a call off to the reddest database and go and pull out all the information.
[00:03:03] When it finishes, it sends us back our profile data. And now we're finally ready to end our response by sending that JSON data back to the server. This is a fairly complex asynchronous flow with 10 or 15 different steps. And if we were to step into each of these functions like if I were to go and find the function to retrieveProfile.
[00:03:27] Here's that function and you'll know here's me calling off into my reddest database and musing more of my sync and here's I'm pulling out all the field names and properly defaulting their values and stuff like that. Any one of these that we went into, there's complexity there to the synchronicity, this is fundamentally an asynchronous program.
[00:03:47] And that really is the way most of our programs are written but we're just used to dealing with things thinking about things in terms of synchronicity. So the point here is that the attempt has been made to create code, that if I wanted to reason about the overall flow, for instance, let's say there was some condition where I said, you know what?
[00:04:07] I need to insert some step in the middle of my flow. I wanna have a nice clean place for me to go and reason about what steps one through 15 look like so that I can easily insert step 7. I know what step 6 looked like and what step 8 expected and I can easily insert step 7 so that handles that flow.
[00:04:28] Any abstraction library that you choose to use whether it's a sequence or queue or CUJO or any of the other ones that are out there, there's a whole bunch of great ones out there. Whatever you use, the goal should be for you to express code in a very declarative way so that you can understand your asynchronous flow control.
[00:04:46] If you're struggling to do that, you're probably not using the right set of abstractions and you might look for something better. Okay.