Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:18Length:1.9 hours
Introduction to koa javascript framework 400x277
  • Overview
  • Transcript

3.9 Getting Tasks From the API

Now we're ready to actually start really getting this application up and off the ground. So we have our service, we have our repository, we have our in-memory task repository. We're ready to start wiring this all the way up to our routes. So now remember, when we first created our tasks route, we were really just returning some kind of boiler plate responses. Now we're actually ready to start doing some work with this. So now what we wanna do in our tasks route is first create a new instance of our service. Because that's what we wanna use to do the interaction with the repository, handle logging, do all that good stuff, so let's do that now. So the first thing that I'm going to do is, underneath my router I'm gonna create a const, and this is going to be my service. And this is going to be equal to a new instance of my Task service. Now in order for me to create this, I need to pass in an instance of a task repository, or more specifically, an ITaskRepository. Now in this case, I only have the one, so I'm going to create a new instance of my InMemoryTaskRepository. This is where if I were to make a change to my implementation and switch to a database or a web service, or switch between MongoDB and Oracle or MySQL or whatever, all I had to do is change this line right here to use whatever repository is necessary. So now I have an instance of my service. I'm gonna handle the get first, so I'm gonna actually handle the get all first. So when a request comes in to get all of the tasks, what I wanna do at this point is I wanna be able to get all the tasks using my service and then return them back. Now the interesting thing here is this is where all of that async really starts to come into play. Now imagine if you've ever done any other development using web services, either using TypeScript or JavaScript, you know that if you start to make web calls or service calls, those are typically asynchronous operations. And what do those requests typically look like? Well, you're typically going to make a request to our service. And let's see what that looks like right now. As we would say service.getAllTasks. Now typically, what would happen at this point is you might have to pass something in as parameter, or not. But then you're also gonna have to pass at least one function that's going to handle this successful retrieval. So what happens when we successfully get all the tasks? You're gonna pass in that callback, which is gonna be a function, which might take in some parameters, which has a body. And then maybe you have other things you're doing in there, and that's probably for the successful operation. And then you're gonna have to pass in another callback for the error, or maybe there's a file. So really, what you wind up doing is creating a lot of nested operations in here that are really kind of painful to manage and read. Especially if you're looking at new code that maybe is a little bit sloppy or something like that. You really get into the what's referred to as callback hell, where you are simply going down this chain of callback function after callback function. And a lot of times, they become nested. Well, the nice thing about this asynchronous and await, instead of me having to pass in a function here that's going to be that callback, I'm simply going to create a value here that's gonna be tasks, which is gonna be a constant. And all I have to do is say I want to await on this. And I'm going to be getting back a value, which is going to be a list of tasks. And then I can simply return it to the end user. So in this case, I can say my cxt.body = tasks. Now what we've done here, instead of creating all these nested values, is we're simply creating what looks like a very iterative implementation where we can go right down the line using this await keyword. Cuz really, what this does as it takes the place of having to nest all of these callback functions. And we need to pair it together this async and await. And what's going to happen is this is going to become an asynchronous operation. And everything is going to kind of be hinged under return of this particular value, and then we'll continue processing. So what else do you do in a router when it comes to a co-application? Well, as you can see here, we're returning a body. And typically, what you would also do in here is any other sort of checking to make sure that the operation completed successfully. When you do that in a REST API like this, you're also gonna wanna return a status code, an HTTPS status code. So in this case, we're gonna return a status that says we're equal to 200, which means this was a successful operation, this completed okay, and here is the results as the body. So let's go ahead and save that. So now we actually have an actual implementation of our get all route in here for all of our tasks. Now there's nothing in there yet, but let's go ahead and take a quick look at what this looks like from our implementation. So if I were to come in here and take a look at my terminal and we have restarted, and I can come back over here and refresh my list. And as you can see, I get back nothing here, an empty array. So I actually did get something, but I did get back an empty array here. And if I take a look, you can see that the get request came in to /tasks. I'm doing the getting all tasks log line that I did within my service. And then I'm sending back a response, which right now happens to be empty. So that's where all of these things are just now starting to come into play. So now that I've got these things started to wire up a little bit, I can start to handle more and more operations on this web server. So on the next lesson, I'm gonna handle the creation in the post, as well as the individual get, and then we'll also bring Postman back into the frame.

Back to the top