- [Instructor] So so far we've looked at a couple of ways
to execute asynchronous tasks sequentially.
We also looked at a way that we can execute
asynchronous task as promises in parallel.
But what happens if we want to combine
both sequential and parallel operations?
Sometimes your promises may contain large tasks
that eat a lot of resources or take
a lot of time to process.
Running everything at once can be too costly,
but running one task at a time may take too long.
A possible solution for this is to create a task queue
that can run a specified number of tasks concurrently
at the same time.
Let's implement a promise queue that can run
a specified number of promises at the same time.
For this example I'm inside of our exercise files
under chapter one, chapter one, less eight
and within the start folder you'll see
that we have the delay promise
that we created that will create a promise
that will delay for a specified number of seconds.
So here we have a list of tasks
and we're using the delay function because we're acting
like some of these tasks take really long,
like on line 13 we're gonna delay
for 10 seconds, a few of them are only three seconds,
some of them are four seconds.
We can use delays or read files, write files,
fetch, any promise would be able to go inside
of this task list.
So what we need to do is we need to make
a concurrent task queue.
And what I'm gonna go ahead and do is create
var delay queue and we're gonna call this
a new promise queue.
And we'll pass in the task that we wanna execute.
And then the next thing that we're gonna pass in
is how many tasks we wanna execute at once.
Let's say that we only wanted to execute
two tasks concurrently at the same time.
And then we would tell our delay queue to run.
Now you're probably wondering where our delay queue is,
whether or not we need to install an NPM,
no, we're actually gonna build this promise queue.
So I'm gonna come up here and just create a new class
and we'll call it the promise queue.
And inside of this promise queue we'll have
a couple of functions.
So we will have a constructor and our constructor,
right, this is function that we evoke here
on line 26 so we know that it needs
to take the task, I'm gonna call the task the promises,
it also needs to take a concurrent count,
how many tasks we should run at the same time.
We'll go ahead and default this to one
in case it's not supplied.
We're also gonna need a little getter,
we're gonna do a getter for a run another
and this will tell us whether or not
we're gonna run another task.
And then we also need to add our run method.
So, this is gonna be our class for our promise queue,
our delay queue is gonna be an instance
of the promise queue, let's go ahead and write the code.
So in the constructor I just wanna save
a bunch of variables to this instance,
we'll say this dot concurrent equals our concurrent count.
This dot total is gonna be the total
number of promises that we have,
we can find that in promises dot length.
So we also need a to do list.
So this dot to do are the promises that we need to do.
So these are the ones that need to be invoked.
We'll be removing tasks from this list.
We're also gonna need an array
to hold this dot running, or the current task
that we are running.
So we'll have an array of those
and then we're gonna have another array,
this dot complete and this will hold the completed tasks.
So these are basically three arrays.
The first one is an array of promises,
that's our to do list, that's the tasks
that we've passed into this function.
So as we start running these tasks
we're gonna move tasks from the to do list
two at a time to the running list
and as they're completed we're gonna move them
from the running list to the complete list.
So we're just gonna manage these lists
so that we can get all of the tasks completed
running them concurrently two at a time
because we've specified two as the number of tasks
that we want to run at once.
So for run another, this is gonna tell us
whether it's time to run another task.
I'm gonna go ahead and return this dot running dot length.
So I wanna make sure that this dot running dot length
is less than this dot concurrent.
So we wanna make sure that this dot running dot length
doesn't have any more than two tasks at a time
and we also wanna make sure that there's still items
in the to do list, this dot to do dot length
will tell us that.
So here we just have a Boolean value
that's gonna return from this run another getter
and it will tell us whether we can run
another task or not.
So let's go ahead and run our tasks concurrently.
We will do this in the run function.
So we'll go ahead and say, while this dot run another.
So while run another returns true
we will go ahead and run another task.
So let's go ahead and grab our promise
from the to do list.
So this dot to do dot shift will remove the promise
from the to do list and set it as the variable on line 34.
And then I'm gonna go ahead and run it.
So we, by pulling the promise out
I can say promise dot then and once this promise
is resolved we can go ahead and say this dot complete
dot push, this dot running dot shift.
So again, the shift moves it out of the current list.
So we've moved the promise from the to do list
and we're gonna actually also put that promise
this dot running dot push into the running list.
So we've removed a promise from the to do list,
we've pushed it into the running list
and then when it's complete we're removing it
from the running list and pushing it into the complete list.
Now the only other thing that we need to do
is when this promise is complete
we wanna make sure that we invoke this dot run again.
So here is our promise queue that can run
a certain number of promises concurrently.
The only problem is is we can't visualize this queue
just yet, so we're gonna actually need to add
some more code in order to do that.
And we'll do that in the next video.