Learn advanced node js, Logging concurrent tasks

In the last video, we created a promise queue. Let’s set up a way to visualize this queue in the console. What I want to do is install an NPM called log update. So I’m goanna come over here to the terminal and type NPM install log update. And what log update is goanna do is it’s goanna allow us to keep the updates that we want to log to the console in the same place. So they will over write each other as opposed to seeing continuous console logs scroll down our terminal.

So I’m goanna come up here to line one, and I’m goanna make sure I import that. Log update equals require log update. So we’ll use that to manage the console log. And then I’m goanna create another variable called to X. And what to X is goanna do is it’s just going to return a capital X. So what we’re goanna do is take every item in the queue and we’re goanna represent it with a capital X. So I will use that to X function inside of my logs. So I’m goanna go ahead and create one more function inside of my promise queue and that is one to graph the task.

So I’m just goanna come over here into my graph task and I’m going to go grab these lists. So we’ll take a list at the to do list, the running list, and the complete list. And we will grab these from this instance. So just a little bit of destructuring there. We’re goanna go ahead and grab those three lists, so that we can log them to the console. So we’re goanna use the log update, and I’m goanna use a template string. The nice thing about template strings is they honor white space, so I can kinda really make these logs stand out. So let’s take a look at what’s in our to do list.

So our to do list will be an array, our running list will represent as an array, and our complete list will represent as an array in this log. And what we’re goanna do is just take these lists, and display a series of X’s. So I will go ahead and say to do dot map, to X, and I can go ahead and just copy this code right here and I will paste it in running and complete, and replace this with running, and replace this last one with complete.

So we’re actually goanna go ahead and graph the task within our run method. I want to graph these tasks when we call the run method, so when we push anything into running, we’ll call this dot graph task. And we also want to invoke graph task after we move anything into the complete array. So I will also call this dot graph task. All right. So now we’ve completed our promise queue. It should give us information on which of our tasks are actually running, and which ones are complete. And here’s our task list that we sent to that promise queue.

Let’s go ahead and watch this work. I’m goanna go over to the terminal and type node dot. And we can see that we have a lot of tasks to do, two at a time are running. As those promises resolve, we remove tasks from the to do list to the running list, and then to the complete list. So this promise queue class we created will work would any array of promises. And it’s a queue that we can use to help manage running tasks concurrently in our node JS services.

Leave a Reply

Notify of