Here is one more example of a synchronous question, but a little bit more advanced, as you can see
here, we have a question execute the given list of asynchronous functions in parallel and return.
The result is in the race to the callback.
As you can see here, we already have three functions and this is given with the task.
So each function have here inside the callback, which returns one, two and three.
Most important, but here that first function is three seconds, then two seconds, then one second.
And then we must write the function async parallel where inside were throwing an array of asynchronous
functions, which means every single function has a callback.
Enter the second parameter we have here own callback for a single parallel.
Here we are getting our result and we are console.log in it here and we must get in this case, one
two three because it must print results or feature asynchronous function in order, which actually means
where execute executing three functions in parallel, and we're waiting for all of them to complete.
And after this, we have the correct order of the results, which actually means we are getting results
of every single function and actually we're using parallel fetch and, for example, quite a lot every
day because we want, for example, to fetch three different requests simultaneously and then get all
data in one place and merge it somehow.
This is why let's build dysfunctional, but it's not easy, and we really need to think what we are
doing here.
So we want to implement a sink parallel function and actually here we get getting first of all and a
ray of asynchronous functions so we can right here, I think thanks.
And the second parameter is our callback.
And importantly, this is a callback for a sink parallel, not for the specific async func function.
And first of all, here I want to create an array variable, put our results.
So here I can write a result array.
And here we want to write new array and inside async functions dot length.
In this case, we are preparing the empty array with three elements.
And obviously it is three only for our case, and it can be any length of our asynchronous functions.
After this, I want to create a result counter and by default it will be zero until the length of our
array.
Why do we need that?
Because actually every single asynchronous function can be ended in random time, and we must wait for
all this function to complete.
This means that we must count how many functions are already completed until we are rich in length.
This is why here let's create our result.
Count them, and by default, it will be zero.
And now we want to look through our asynchronous functions.
So here are some funnels dot for each and really using follow up here and we get an access for every
single asynchronous function.
And we also need here an index because we need to store the results also in the same correct order.
Now inside for each, we must call our asynchronous function.
Most importantly, we don't know at all what this function is doing, but we know that this function
gets a callback.
This is why here we can write our callback.
So we're calling a single function without any arguments.
And inside were providing our comic if this callback is triggered.
This means that we are ready with our asynchronous function and now we want to write the result inside
our brain.
This is where here actually, we're getting in our callback value.
And as you can see here, inside every single callback, we're getting a value.
And this is exactly the value that we're getting inside our callback here.
Number one to store this value inside our result today.
So here we can write results, right?
And here will be our index equals value.
And just to remind you, whichever index here, this is why we know that was stored it correctly.
For example, this is the third function and it will be triggered first.
Then we will write it on the index.
After this, we must increase our result counter to know that one of the functions is done.
And last but not least, we want to check if this function was the last one.
So here we can check if our result counter bigger or equal async function.
Then we want to call our callback for a single parallel.
So here we can simply write, callback and inside were provided our result array.
So once again, what we are doing here?
First of all, we are preparing an empty array and our content.
Well, looping through our asynchronous functions and we're calling every single function.
And here we are, providing in every function, a callback.
And when we're getting success with, first of all, store our result in result index.
And then we increase our content.
In this case, we know that this logic will return us callback for a simple parallel when we're ready
and when all our functions are completed.
So here we are, calling our callback with results right now.
Actually, we must move this function before our call async parallel because we must write it in Wisconsin
and let's check if it's working.
I'm relied in the page and we're getting an error assignment to constant variable.
Let's check the result.
Yes, as you can see, the result counter can be increased because it is const.
So here we can write led and then it should work.
Let's check the sound.
I'm reloading the page with don't have any errors.
Now, three seconds passed and we're getting our array was one to three, which actually means here
are a sync.
Parallel waits for every single asynchronous function to be completed.
And then we generate as a callback our result here and now we can use our array in the correct order.
And this is completely fine if you can build this function fast.
Most importantly, if you are doing it in the interview, you must always say out loud what you are
doing and how you are thinking, because normally on the interview, it's not that important.
If you solve this task fully or not, you must show your way of thinking and how you want to solve this
specific task.