This course has been updated! We now recommend you take the Complete Intro to Real-Time course.
Transcript from the "Using Asynquence" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Kyle Simpson: Now we get to clean up some more code let's go back to 2.js. And you know how we were passing in this crappy looking function here, with all the if error condition stuff. Nobody wants to write any of that code, so let's take all of that code out.
[00:00:20] In fact we don't have to pass in a callback at all because the say function is gonna return us our sequence back. It's like returning us a promise that we can chain off of. So now I know I'm gonna get some contents back from this thing and I know this is the final step in my sequence.
[00:00:37] So I could do then but an even simpler way is to just do .val because,
>> Kyle Simpson: If I had done .then, which is the more standard thing that you see in the API, .then always gets us this first parameter, that trigger and then any success messages after that.
[00:01:01] So rather than typing out the done and getting that parameter that I don't need anymore, the shortcut that's provided is just typing .val. So it assumes that we're going to just have a synchronous step here that we don't need to do any more asynchronicity for this step. So we receive the content into this function.
[00:01:21] Now we are not handling the error here are we? Does that seem like something that we've lost? Well it turns out that all the promise abstraction libraries they all have a way of handling it. The way asynquence handles it, it allows you on any sequence to register an error handler by calling the .or.
[00:01:43]
>> Kyle Simpson: So anywhere in the sequence that an error occurs, it will bubble up to any registered or handlers on the sequence. So anywhere, if it had an error reading the file, if it had an error doing the delay, if it had an error anywhere. Any kind of error that it encounters, it traps those errors and forces them into the error stream of the sequence.
[00:02:03] So we can safely do console.error and print out our error.
>> Kyle Simpson: If you do a diff, if you do a comparison between the 1.js and the 2.js in your Hello World and your Hello World 2, you'll see quite clearly we deleted a whole bunch of code. Because we're taking advantage of the power of promises in those abstractions to reduce the code.
[00:02:34] So that all that's left is the stuff that we really care about which is just the expression of our flow control, so that we can manage it properly. Here when we look at this sequence we say, what I need to do is call hello.say and then when it finishes I either get the contents or I get an error.
[00:02:53] And that's how we would think about our asynchronous flow control from this level of abstraction. The goal is to create nice maintainable levels of abstraction around your asynchronous flow control.
>> Kyle Simpson: Save out this file. Try it from the command line node 2.js. Remember what happens with if we don't pass anything we get immediately a problem there.
[00:03:20] So let's pass in a -- file=test.txt and we wait one second to print the output. Now what happens if we get our error, we give it a filename that doesn't exist? We immediately get our error. That flowed all the way up the sequence chain into our or handler exactly the way we would have expected.