Check out a free preview of the full The Good Parts of JavaScript and the Web course
The "RQ Function Types" Lesson is part of the full, The Good Parts of JavaScript and the Web course featured in this preview video. Here's what you'd learn in this lesson:
RQ has four types of functions. They are requestor, callback, factory, and cancel. Doug spends a few minutes looking at each function type and their use cases.
Transcript from the "RQ Function Types" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Douglas: RQ has four types of functions in it and if you can manage these four types of functions then it's it's pretty easy to use. We have requestor functions, which are functions that can execute a request. We've got callbacks, which are continuation functions that are passed to requestors that's how requester returns its result.
[00:00:26]
We have factories, which are like the factories you've been making which make requestor functions, they act as a convenience. And in fact the four functions that I've shown you are all factories and then cancel functions that cancel can be returned by a requestor to cancel or request. A callback function takes two parameters, a success parameter and a failure parameters.
[00:00:49]
If success is undefined then it fails and the failure is undefined when it succeeds and there's just one callback in promises for example will be two callbacks one for success, one for failure. I think it's easier if you just have one. Than a requester function will take a call back and it can optionally also take a value and that's how a sequence will move results from one step to the next.
[00:01:17]
A requestor function can return a cancel function which is used for cancellation and factories make requestors. That's the relationship of the four kinds of functions.
>> Douglas: Let's look at an example. This is the identity requestor this is the simplest possible requestor It will receive a value and it will then send the value to the next thing in the sequence.
[00:01:46]
If you put this in the middle of a sequence, it's a do nothing it will just take whatever's going on and give it to the next one. You never do that but this is the model for the simplest form.
>> Douglas: We can wrap those things or, I'm sorry, this is the fullname request this one receives an object containing parts of names and we'll concatenate them together to make a name and then deliver that to the callback.
[00:02:25]
I could add this as a processing step for example. And we can write functions which will automate that for us, so request arise is similar to functions that you've been writing. It will produce a requestor that will call some ordinary function. We can take any existing function and turn it into a requestor which could reduce the amount of work you have to do in transiting to RQ.
[00:02:58]
>> Douglas: This is a Delay Requestor. This is the simplest real time thing. You would never put a delay into a server application. What you would do in a real situation is instead of calling setTimeout, you're going to send a message to some service and instead of calling clearTimeout you would send a message probably to the same service telling it to please stop.
[00:03:22]
You just make the substitution but if you were to take the delay requestor and put it in a sequence, the sequence will run exactly the same it will just take longer but it will take longer without blocking. If then this is the Delay Factory. It's this code is the delay requestor except the factory is providing the milliseconds value.
[00:03:49]
>> Speaker 2: These are all part of RQ?
>> Douglas: No these are examples of how you could use RQ.
>> Speaker 2: Things that you put in there okay.
>> Douglas: Now know that this code is.
>> [CROSSTALK]
>> Douglas: Request.
>> Douglas: Then this is a factory for reading files. This factory will produce a requestor which you can then put in a list which will then read files for you and this one is wrapping the node file system.
[00:04:26]
This is the widget that was in the demo that I showed you earlier. This is all HTML crap, so I'm not gonna bother you with that but and I did it this way cause I didn't want to have to depend on any one library because if I chose a library that's not everybody's favorite, and it turns out everybody is not somebody's favorite so it's just, so it's awful.
[00:04:52]
But the interesting features here are that this is the quick handler or the success handler, so when I clicked on yes on success, I'd changed the color of the widget to green and I called the callback to let it know that it finished successfully and on the failure side was similar except I set the background color to pink and success is undefined now to indicate that we have a failure result and in the case of cancellation, I just set the background to gray.
[00:05:31]
>> Speaker 2: Why'd you reverse your success fillers conviction node?
>> Douglas: Because node got it wrong.
>> Speaker 2: Because it got it wrong?
>> Douglas: Yeah
>> Speaker 2: Okay, I agree that it seemed really weird but now you start to use to hire people who do it the right way it seems weird.
[00:05:47]
>> Douglas: Yeah, exactly and I know I mentioned this in the hazard, doing things right in a system that does things wrong.
>> [LAUGH]
>> Douglas: It's going to, yeah, and I understand that. I apologize for it.
>> Speaker 2: Okay.
>> Douglas: I just couldn't bring myself to do it the wrong way.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops