- [Narrator] In the last few lessons,
we discussed sequential execution.
But promises also come with some functions
that allow us to resolve them at the same time.
We can make a bunch of asynchronous requests at once
and obtain a series of asynchronous results faster,
because these requests will not have to execute in order,
or wait for the previous request to be completed.
When we're resolving several promises at the same time,
it's referred to as parallel execution.
Let's go ahead and take a look at our sample.
Now we still have some of the same code
that we created in the last few lessons.
Using the file system and promise of file utility,
we've created promises for writeFile, unlink, and readdir.
We also have a beep function on line six
that will cause a beep to occur,
and then we have a delay function
that will return a custom promise
that will wait for a certain number of seconds.
We can create a single promise
that will resolve several promises at once
using promise.all.
So promise.all will take in an array of promises to resolve.
So we'll go ahead and writeFile.
And go ahead and write a readme.md,
and we can go ahead and write a hello world
message in that file.
We'll write a second file.
We'll go ahead and write a readme.txt.
Also write a hello world message.
And let's go ahead and write a third file
and actually let me make sure I spell writeFile correctly
across all three of these.
And the third file will be a readme.json,
and we'll just go ahead and put in a
hello
world as json in this file.
Great.
So now the promise all returns a single promise
that will be invoked once all of these promises
have resolved.
So we can then handle the result of
all three of these file writes in one place.
Let's go ahead and return a readdir.
And we will just go ahead and console.log
whatever the results are from the read directory.
So this code will wait for all three write file promises
to resolve before reading the directory
and logging its results.
So I'm gonna come over here to my terminal
and run this by typing node.
And we can see that we have a list of files
that are in that directory.
Now if I wanted to remove these files
I can use my unlink promise.
So I'll just go ahead and replace these with unlinks.
And all we have to send the unlink promise is the file name.
To really get a sense of how this works
I'll go ahead and add another promise, a delay.
So we will delay for three seconds.
So now we have three unlinks which will remove the files,
and a delay.
So we're gonna wait three seconds
because it will take three seconds
for all of these promises to resolve.
So let's go ahead and go over to our terminal.
Node.
And we can see that we have a pause,
and then we see the files that are in the directory.
Readme.json, readme.md, and readme.txt
no longer there because they have been removed.
So let's consider a bunch of long processes.
Let's say that we had a delay,
five,
a delay,
two,
and a delay,
we'll do another delay five.
So here we have some promises and a couple of them,
the first and last one,
are gonna take quite a while to resolve.
So I can go ahead and run this in my terminal,
and what we're gonna notice is
we're not gonna read the content of the directory
until that last promise has resolved.
So until all five of them are resolved.
But there is something else that we can do.
We can also execute promises in parallel
by using promise.race.
So promise.race still creates a single promise,
but instead of waiting for all of the promises
to be resolved before the single promise is resolved,
we're only going to wait for the first promise
to be resolved.
So let's go over here and try to run this.
Node.
And what you'll notice is we still have a two second delay,
but then we see the results of that promise
before it has completed.
So promise.all and promise.race give us a way
to execute promises in parallel.