Welcome back.
Today, I will discuss two new S9 features, they are not new at this stage.
They were new when they were released.
Right.
So I'm now in 2020.
So it's not Estyn, but they they're new for you guys because you don't know that.
Now, the first feature is called Finally.
And the second feature is called for a wait.
Now you're going to get these two lectures, the first lecture from the previous class, from the class
before in this lecture as well.
So you can go grab this one, sit back, relax, listen to me what I'm doing, or you can start coding
together with me now.
Now, first things first.
I want to see that we need to write something.
We need to call our logic for fetching and dealing with different promises.
And then I will introduce this finally here.
OK, now I'm going to use the different apps.
The first Swaby.
The second is Jason Placeholder.
We already have done this now the Starwars API.
I'm not sure if I told you, but is doing a data about people, starship's and planets.
OK, so copy this one or go straight to swapping out there and it will go here.
Right now I can grab a different starship name model, whatever it is there by changing the number here.
But not every number is working.
So if I type one I will get for four or four not found right now.
The promise was fulfilled.
Well, it didn't file.
It didn't return anything.
You know, I've just tested out and I'm going to start doing this.
First things first.
Let's create a U are l actually let's create multiple URLs and store them in honor of NAREIT.
So let's create that array in called star ships.
You are else, right.
So it's going to be array here.
Here we are going to store multiple what you URLs so we can fetch data about it.
Now I'm going to put here a six and one, two, three, four, five.
And I forgot the comma between them
actually on the last one.
I don't need it now.
Here, I'm going to change the only the number.
So three is working, five is working, nine is working.
So in future, if this is change, please go to this side and test is out now.
Ten, twelve in 15.
OK, now we have the new or else that what we need to work on.
The next thing we need to do is create a promise in here we go.
I'm going to use promise that oh for the first time so I looking I can loop through all these starship
you URL's using the map function.
Now remember the map function what it does.
We have used it a hundred times so far.
Right now I'm going to write it down and after that I'm going to explain it to you.
So promise that all in inside I'm going to pass the starship.
You or else I define it here.
Cool.
I'm going to loop through this with the map function and I'm going to pass the you are out here.
Actually, the euro will be the element that I'm I'm going to get at the current element so you can
put here whatever name you want.
But that is what we are going through a single URL.
So that's why I put URL here, right in inside here.
What we have return fetch.
So we're going to return that.
That's your URL and then ships because we're going to get ships on the right ships that Jason and this
is a method.
OK, what's next.
So I'm, I'm using almost all the fitting.
Right, using too many dents.
But it's OK, don't worry about it.
We're going to do something more modern a little bit later.
So here I'm going to access the array and I'm going to cancel up the information.
Now, don't get confused.
Why?
Because the arrays are starting for position.
What zero right now.
I'm going to copy this five more times.
Maybe I've got a bit more.
So this will be two, three, four, five and six in here.
We need to start from zero because that's how they already are working.
They're indexed in the next hour.
It's always from zero
five.
OK, we will see if this is going to work just in a couple of minutes.
Then I'm going to do one more.
Here, I'm going to catch you if there is an error.
Now, I'm going to parse the error here, but I'm going to cancel it and I'm going to write something.
Uh, there was some kind of error.
What it could be.
Right.
Questionmark.
And also what you can do.
You can also define the error here.
Right.
Good.
That's the error still we are using.
Right.
But you don't have to.
Don't worry about it.
Now, let's keep it interesting.
Open a new tab here, inspect Kosal and let's paste this down and hit enter.
OK, good sign.
Promising spending.
And here is I got six returns here.
Six different objects, different names.
Stardust were in this account.
That star Millennium Falcon.
Very good x we executer nice but we know all of this right.
We know how to use promises, how to consume them using it then it's a little bit outdated.
Right.
But I'm going to use something here called Finally and what finally means you need to understand this
only this.
The finally named index indicates they will do something at the end when it's final, when that promise
is back, or would that promise has returned.
Right.
So you need to know the final.
You will always be called regardless of that promise, outcome, outcome.
The promise can be fulfilled or rejected.
Right.
But the final will always be run.
Now, how are we going to use this finally here right now?
Let's work a little bit here, so I'm going to define the finally here, I don't need the semicolon
actually anymore because I need to write to the keyboard finally here.
Right.
And inside the finally, I will use this as no parameters.
And I'm just going to control the lock and I'm going to explain what's happening.
Finally, I want what's happened here, I want to print something good.
This is your message.
This is finally what you want to see here, finally what your app needs to do.
Right?
That's going to be your logic from now on.
Now, here, I don't pass any data, any parameters you want, because the the promise doesn't send
any data to the family so you can print whatever you want.
Here you have one semicolon.
Now, let's try this again.
I'm going to copy the entire thing.
I'm going to delete this and refresh.
Right.
Because it's not going to run if I do the same thing.
Here it is.
Finally, I want to print something, OK?
Now, I know that finally, even all the promises are fulfilled.
Execute it normally as they shoot no errors there.
They will be the final will be the finally function will do its job.
OK, now we know that finally is working when there is no problem.
Let's throw an error here.
Let's for example, change the swap to swap is right.
Save it.
Go back.
Refresh.
Remember this one here and here.
It's oh there was some kind of error rate, what it could be.
And you tell us.
Right swap is the reason.
Of course request did not succeed in here it is.
The finally function again is fulfilled again.
Is there.
Nice.
But I've never shown you this before.
What we can do, we can just force this to throw an error here.
Right.
So force that didn't matter to throw an error.
So how we can do this.
OK, let's let's use it here.
So the keyword is throw and I will say here error.
Nice.
Save it.
Copy the entire thing again.
Refresh here.
Base it back.
Hit enter.
Oh there was some kind of error.
What it could be here is finally is working again.
Now you know how to throw an error intentionally.
Right.
Without changing the APIs here or adding something that doesn't, it's not going to proceed successfully.
OK, now that is pretty much about the finally and it is useful when we want a piece of code to be executed
in the end, no matter what, no matter what the outcome of the promise will be.
So this will run no matter what, regardless of the outcome.
And it's very powerful because of this.
For example, you want to send some some users are trying to log in somewhere.
Right.
Is trying to do something here and is something is wrong.
And with the request that he's doing, he's sending.
And so you want to use some sort of notification.
Right.
Or imagine that you need a notification to the user when something else is happening.
For example, they do they call log dictate.
They are asking for some data that will not be able to access.
And yeah.
So instead of catching the throwing in or you can also use the finally and do whatever you want dear.
So the piece of code that you have in mind, it will be executed here, cuz the next big thing that
we need to discuss is actually for a wait and this is a little bit more advanced for this.
We will need to look through multiple promises almost if we are writing an asynchronous code.
Right.
So now for the for a week we we will take each item from an array of promises because we can store multiple
array you URLs here promises inside on the rate.
Right.
So we're going to repeat this one, but we're not going to use the swap here.
We're going to use the adjacent placeholder.
And I have actually open it here outside and let's use it.
Right, OK, go back here and type for away.
Not so let's write Jason Placeholder, you or else that will be, again, like the adjacent placeholder,
but you are El's and this will be Oray that will hold all of the different URLs.
No, I'm just going to copy the entire URL from here.
I'm going to stay here and I know that they have for the users, they have a one for post.
They have another one, they have a couple of actually.
But I'm going to use the albums here.
Right.
Nice.
OK, so let's create an async function.
So I'm going to call it get results const get result and here it will be async function but it will
not take any parameters here.
We can close it down here.
Now the creating an array of promises of the array above that later on will be iterable.
So we can go through this, we can loop through this array.
Now let's write here const array of promises and that will be the adjacent placeholder URLs.
Right.
I'm going to use the Genda map method on it and I'm going to grab the U.
RL and I'm going to fetch that.
You are up to something similar that we have done here, right?
So that's why you use the old style.
Now I'm using the async function here.
Nice.
So you have a little bit of practice.
So when our Jason Placeholder URLs, we will use the map method to loop over to you or else from the
as a single parameter here would be passing as a single parameter.
It's going to look through this, through this, then through this.
Right.
And this will return the fetched Eurorail.
So creating an array of fetched promises now for we need to use here after this, we can use the full
weight to loop through all of these promises here.
Right.
Stored in disarray.
So all of these promises will be stored here and we can use this for a loop.
OK, the forward syntax is like this for a like that.
And let's request off.
And here we need to pass the array of promises because that is actually the array of promises.
We are fetching something and we are looking through that nice.
So again, we are looping through all of the promises, but now we know how the question is how we can
extract the data.
Right.
OK, now we can create const data here and we can use that request that Jaeson.
But remember, the weight is used in front of every promise.
Right.
Because we are looping through the promises here and I use the request that Jason.
Nice.
So the next thing is how we can extract the data.
Well, it's simple.
We just going to cancel it, lock the data in.
That's pretty much it.
One more thing that we need to do is actually to call get results.
So actually, we can achieve the same same thing is here.
But I choose to use a different because different API because so sometimes these APIs can no longer
exist, actually.
Right.
And I just want to give you as much more examples as I can.
OK, copy this one.
Refresh it here.
Based it on there and hid under the promised spending.
And here it is.
I do have three different arrays.
The first one will be the for the user.
Right.
The second will be for the posts.
And I have 100 posts by the way.
Nice.
And third one will be 40 albums.
And I still have a hundred albums now, ladies and gentlemen.
Now you know how to use the parade and you can look through the promises of a different set of promises.
And before we using a wait, before each and every promise, because I know that this will return the
promise here.
Right now, you know how to use the for a loop.
And if you do this, you're True-Blue.
Right.
OK, see you in the next one.