Welcome back, ladies and gentlemen.
Today, we're going to talk about new iOS nine features there finally.
And for a weight off a off in this, we have done this one in previous.
These two links are for you to read in your free time in there for the finally and for a wait of OK,
so they are MDM links.
And now before we do this, we need to do one example.
We promise that.
OK, so we're going to do a six.
Promises.
And we're going to use a promise that, oh, OK, but for doing that, we need to have this Pokémon
API.
This is a restfully API and we need this link here.
So open up.
Type Pokémon that Apple and Google, and this should be the first thing that you see POC API now from
here.
You just grab the ability, grab this part and create a const you URL's.
This will be an array of four different URLs because we're going to need them so we can loop and let's
grab at this one here ability and we're going to start from one.
OK, so we're going to loop this using the map method.
OK.
And the reason why I'm showing you this, well, because we I need to show you how finally is working.
So three you know, four.
Nice.
Get rid of that comma over there.
Good.
Now go back to your lecture for grap this promise that all and we're going to change it a little bit.
So if you don't know what what about what this promise that always doing.
Just go back, watch the lecture about promises here instead of passing Jason else.
I'm just going to pass this.
You are wrestling here and it should be you or else not you are as crazy and instead of response, just
change it.
The ability because we are we were getting the ability and that's it.
This using map method on this array and we are fetching each and every year on each and every loop.
This will return us for promises.
This is how we just consume the promises and not quite.
Well let's change them.
Let's say Pokémon one.
And let's put a comma here.
So we have four Pokemon's that we need to worry about it right at the moment, we are doing one, two
and three.
Now, I need to call this one, changed this to three because this will return an array of four.
OK, now go back here, make sure that you create lecture and link it in your index file.
OK?
I really don't want to say this, but the students are jumping now.
I do have the result back and I have Pokémon one and there are a lot of things inside.
OK, instead of that, just.
Do this, that name now I do have Pokémon one stench, the ability to stench.
Well, then the other one is drizzle speed boost in battle armor.
Nice.
Now we know the Pokemon's ability.
You can read more about these Apache restfully appear in your free time.
And if it's not working, then use JSON placeholder.
I'm just joking.
Should work.
So what's happening here?
OK, now we are using promise that all and if there is any error here, let's do this through error.
So let's see what's happening.
We will see this error right now.
What we can do, we can do something a little bit better than this.
I'm just going to close this one.
I don't need it anymore.
So the area is working, we are catching errors if there are some things happening.
OK, but what about this finally?
Right.
So how are we going to use this finally?
Well, it's very simple.
Just add one more line here and say finally and inside is finally, let's create an error function in
console lock.
Finally.
Finished, you can put whatever you want here.
And if you save it, you will see finally finished.
So why what is this finally?
So no matter what happens with the promises, finally will be always called whatever the promise is
resolved or rejected.
So does a matter of the outcome of the promise we have for here.
Right.
If even if it was one, this finally will always be executed.
Now, even if we throw an error here, let's do this through.
This is how you're going to throw a force reprogrammed through an error.
Here is the error occurred in this line, was there?
But even this doesn't matter if there is an error.
This one still executed.
So finally, it's doing this.
So that's why it's called finality.
Whatever happened before, I want this piece of code here to be executed.
Right.
Does it matter if the promise was resolved or rejected?
That is pretty much the finally.
And you can read more about the finally on the link here.
Nice.
Now, the finally does not take parameters here, so you can put it here data save it.
And if I conjola the data.
So he's going to give me back undefiled so it does not take a data or from the promise, the promise
will not give access to any data here.
Right.
And somebody will say, well, why we need to use this.
So finally is not getting data, but it runs at least once.
Right.
So this is something that we want to do at least once.
So, for example, you have a piece of code.
And you want to that piece of code to be executed at the end of the promise, no matter what.
That's where finally power is.
So we want that final piece of code to be executed, no matter what of the outcome of this promise is
nice.
So that is pretty much it now for why I give you this promise that all like this when we have learned
the async functions in our lecture seven.
Right.
Well.
Let's do something here, so let's convert the promise that we have back to what to async function,
OK?
Convert the example to async.
Function, OK?
Now, how are we going to do this?
So I'm going to create CONSED and I will say get Pokemon's, you can say get Pokemon's ability here
and this will be an async function, right.
Good.
Now, we just going to call this async function outside.
Get Pokemon's nice, it will not take any parameters.
So here, what I will do is I'm just going to comment before this error because we don't know, we don't
want to throw it copy and commented back.
OK.
Now, let's face it, this one here.
So what will happen?
Well, we need to change a couple of things.
First, we need to have try and catch block, right?
Well, let's first change a couple of things.
Now I'm going to create const.
And I'm going to say Pokémon.
I'm going to create Pokémon, I'm going to create four constants, so there will be for these four things
here.
OK, one, two, three and four, and they should be two.
Three and four nights.
So get rid of this comment and that will be equal to.
Is that OK?
But when we have a promise that we always used a wait right before the promises, we can use a wait
and promise that all but here last time I didn't tell you how we can change all of this and make it
back to look like.
So I'm just going to get rid of this part here because we don't need it now, last time I didn't tell
you how we can change this part to be async as well and get rid of these then Cheney.
OK, so how we can do this?
Well, in order to do this, this is a function that takes one parameters.
Right.
So you URL's up map and we have a function here.
Even if you do this, it will not throw an error because this is a function that takes a one parameter.
And what we can do, we can say async function, because if we want to use a wait inside, this function
needs to be an async and it will not be an error function anymore.
So here what we can do, we can say const response.
Equal to.
Await the fetch you, Earl.
That's it.
So we are done until this part, then I have to return.
Which is going to.
Delete this one, this part, and here I'm going to return the data.
So data will be equal to await response that.
Jason, let me finish this one real quick.
Very good.
So I'll say here, let's cancel log.
Pokémon one, that name.
So now we have the access of the Pokemon's.
OK, and we can see here Volkman one.
Comma there, so we can copy this and pasted three more times because we need four for Pokemon's, so
this should be these should be two, right?
Should be three.
And they should be for there you go.
Now, let's save it and go back to your it is Pokémon stench.
Let's change this one here.
Let's change it to this, because I don't want to be identical as the other one, so some students will
confuse it because they will think, OK, it's the same.
It's not saying same.
It is.
It's working, OK.
Now, if we have an error, remember, we did try and catch block.
So try then we have the catch and we were catching the error with some error occurred.
Then we use the catch block to print out the error.
And pastie earlier, oops.
Very nice.
That's it.
There you go.
Now, if we have some error, but here, make sure that you don't comment about this one because it
will not work.
OK, so that is pretty much it.
This is how we transform the promise that all using that, then, yes, six to is nine using async single
word.
OK, that is pretty much cool because in the previous lecture I didn't tell you how we can change these
four.
Right.
We just left it as it is using that then.
Nice.
So that is pretty much it.
Now the next part that I want to show you is for our weight off.
But before that.
We need to I need to explain for off loop again, OK?
So we basically we did get rid of the then Cheney and made our promises to look very nice right now,
the new feature, these for a weight off, will allow us to loop over the multiple async await calls.
We have multiple async awake calls here.
Right.
And because we are going through we are looping through this array here.
That's why we have a multiple await calls.
Nice.
So before that, I just want to explain what four off was.
Now, four of the Syntex was just like this, and let me just write it for you.
I want you to have it so you don't waste time looking at.
So what is for?
So for now, inside, we have variable.
Of iterable.
And here we have our statement, Loch Ness.
So what is variable on each iteration of value of different properties assigned to a variable so variable
may be declared.
We can't let were whatever you want right now of what is the iterable?
The ideal is the object whose eatable properties are iterated.
Now, this sounds this might sounds confusing, but let's give you an example.
So I'm going to create a const function called for of loop and it will take the you or else.
These are here of Earles, right?
Nice.
So what we will do is we will use for our loop and I will say let invariable will be you are out of
your house now, it's getting better, right, and console.
So that log and we're going to see you around nice.
So again, now variable iterable are very easy to understand.
So.
On each iteration of value of different property is assigned to a variable, very good, a value means
this value, right, for different values.
Now.
Iterable is an object whose iterable properties are iterated, so this is the object that we are just
looking through right now.
And here we can just use CONSED, let whatever we want, save it.
Let me just close this part.
Let's see.
Here it is.
Now we need to for loop just to call it outside and pass you or else.
OK, now you have them hdb we have four different URLs.
Now you know how the four of Leupp works, so let's use for a wait.
On this, get Pokemon's.
OK, just going to carpentering for a wait here.
Using.
Async OK now, because we already have Pokemon's now, if you don't want to use other name, you can
comment this one, but I don't want it.
So what we can do, I can just.
Do this part, what's happening here?
Probably I didn't copy everything, let me just.
Copy this part again.
Because I have somewhere mistake, but that's all good now.
Now make sure that this is called one and this is called one, they're OK because we need to call it
with different names because we're going to have a problem.
This is a concept.
OK, now let's use this for a weight loop.
So let me just.
Get rid of a couple of parts here, and I'm just going to make your life easier now.
So if I do this, so what are we going to do?
Let's create an array of promises and let's call this array of promises, same promises, array and
in disarray.
We will look through the OR using the map method.
So you earles that map.
And inside, I will have you, Earl, and fetch.
Let's make this one bigger fetch, you are out.
OK, so what we have.
We create the array of promises and we look through the world using the map method.
Now the map method will return a new array to us because every map method returns an array and it will
be stored in these cost promises.
So what we are doing is we are fetching the each array each year out link here.
Right.
So let me just show it to you.
So let's lock these promises, Ari, so see what's inside there, so cancel the lock, save it.
And now I have four promises back.
Why?
Well, the fetch method always returns a promise, but I have a few different promises because I'm mapping
through this array, which contains different four different links.
Now, I do have these four promises back, so we don't need this.
And now the for a loop we can use here for a wait.
This is how it is being called.
And I'm going to say here, let promises or let promise off.
I need to
supply these promises.
OK, now const data, I will say outweight promise dot.
Sorry.
Promise that.
Jason Very good.
And finally I can console lock that data and I can use that name.
OK, now what is happening here.
So we kind of lock, we see that inside this array we have a four promises and we see that the four
hour loop was working very good with those things.
So here we create the variable in iterable.
So here is the scene.
This is the variable called promise.
You can call it anything you want and this is the iterable or the entire rate that we want to look through.
So every promise that is saved in this promise is now we will be able to loop with a wait for a loop.
So to extract here the data now we need to use a wait promise that Jason.
But while we wait while we use a wait in front of promises.
Right.
Because this year we are just accessing each promises in the right.
Now we are awaiting that promise that Jason to resolve and after that, we just counseling one, save
it and let's see what's happening.
There you go.
This was the previous output.
This is the new output.
OK, and we have OK, here it is, the first one.
Now, what's happening with this for a loop or with any function?
We will get the data when it's ready.
Right now, this was before that happened before I get the data.
That's why the stanched save it again.
Now I have stange result.
Then the other one.
When I said the other one, it's coming from these get Pokemon's, OK?
And there you go.
Now you will not be able to see.
For example, line each to be ordered one by another, well, because this is away, it's not going
to block the code.
So if it needs to wait, that's what happened here.
So you've got this two, then you need to wait to get the speed boost in battle armor.
But before it's waiting, other other functions were executed.
That's why it's called asynchronous non blocking code.
Right.
Don't get confuse.
Our functions are working as they should, so they are not blocking the code.
So they are resolved when they get the data.
So here, if I save it again now again, I'm going to have the same one.
OK, let's do it here.
Let's just make sure that this is smaller.
So save it one more time.
Here it is now.
It's different now, but save it one more time.
Different output.
Yeah.
Here it is.
Now I have the first function here.
Get Promis, get Pokemon's.
It's been executed and it gave us the data then the second second for a weight.
So this is, this is with the full weight that was executed and gave us the data back to us.
Right.
That's pretty much it.
So for a weight takes each item from the array in weights for it to be resolved.
Sometimes this can can happen and get the first response.
But the second and the second response will be not ready.
But at the end you will see all the responses right in there in the correct order.
Right.
So even if this was moved on top, they're incorrect or a stench drizzle.
It will be like this here.
It is stange result.
But then we have the last two.
OK, these are the features that today we discussed.
There were finally and we can put here finally.
Where was it?
Well, I went too much, so finally it executed no matter what.
So this is good for a piece of code that you want to be executed at the end of each promise.
Doesn't matter if it's fulfilled or rejected, then we have for all we have done a for example, and
after that we have done for a weight off.
OK, so this was for a weight off.
And look how this is cleaner than this one right now.
If you want, you can say here Pokémon, but you can change this cause a little bit.
Not to be nicer, but look this part and see it here.
Right.
That's pretty much it for this lecture.
I hope you like it so we learn about new or latest feature of its nine or 12 and 18.