Welcome back.
Today, we're going to learn about E.S. 11 or is 20, 20, so this ECMAScript was released in 2020.
So congratulations, ladies and gentlemen, you are very, very successful.
So we started from the scratch and now we are here.
But today we're not going to jump straight to iOS 11 features.
So I'm going to go back to Essex and I'm going to mention four methods for promises that we haven't
covered.
And we haven't covered this for a reason, because in the year 2020, there is a new method released
for these promises.
And I knew then and I knew it now that if I just include it in one place instead of including 50 lecture's
before, then you will pretty much forget about it.
And we don't we didn't use them until now, maybe.
Actually, I've used it only once in, but that was not something very important.
So I haven't used these six features, but I'm going to use it now.
I'm going to use them now.
And I need to show you all of this because I will not be able to explain all of the year 2020 features.
So you haven't missed out anything before.
Right now it's the time to start working on them.
So let's start.
So is six.
So these are your six promises.
So before you six implementation, we have a promise is implemented in a libraries like Bluebird or
Q Right.
Thank now now S6 or is 2015 promises are implemented as a standard standard part of JavaScript core
language.
So we have old method race method to reject and result in.
These are implemented in promises class.
So this is promise class nice so we can make more instances of now promise.
The old method takes in the iterable all the promises as an input.
So that can be a real overpromises.
Right.
And returns back a single promise that resolves to an array of results.
What.
OK, so let's start coding.
So first things first.
I'm going to create const promise one equal new promise here so we make sense of this class here.
Are we going to see whether it will be resolved or rejected?
So I'm going to say resolve or reject here and I'm going to pass the error function in inside here.
We're going to mention the set timeout function right now.
This set time, our function is super important for us.
Why?
Because we haven't used it until now cuz now the set out function we haven't used actually in years
since before we used it, when I was explaining the promise in this set time, I will never feel right.
It will always execute.
It's not like an API that can go wrong.
That's why here we are going to pass the resolve in the milliseconds when they should kick in.
And this is the value that I want to restore.
Promise one.
Let's compare this promise based at two times and change this to two as well here, too,
and the last one to three and change it back to three.
Now, what's the Syntex?
Let's be sure that you put a semicolon there.
You don't have to.
That what's the syntax.
So promise that all in here is.
I told you it takes an iterable.
Overpromises or an array is an input in here.
I'm going to jam all of this.
Promise one.
Promise to and promise three.
Right.
Good.
And what I can do here, I'm going to attach the then method.
And I'm going to simply data and control this data so we can move this one a little bit down here and
I'm going to console lock the data, that is pretty much what I can do here.
Actually, let's not finish it here.
Attach that catch.
And in this catch here, I'm going to parse the error like that.
And simply, I'm going to counsel and I'm going to see error occurred in one of the promises above.
Space here, exclamation mark, and as well, I'm going to just print out the air nice.
So these all three promises are resolved.
So here is the syntax.
Is this Brahmas that all Jammal, the promise is here as an input or as an array?
And here I'm not doing anything special on this, bringing out the data or if it's any error I'm going
to catch.
But I know it will not be error because this is a satima function.
It will always resolve the truth.
Right.
If we don't specify reject here, it will always be true.
But here we need to change one thing.
It's a five hundred milliseconds for the first one.
Make thousand loops.
Make thousand for and a second.
Second one and let's make two thousand for the third one.
These are all in milliseconds.
OK, copied interring.
Go to Google Chrome.
I suggest you to use the Google Chrome based in here and enter now the promise will be pending and now
all of the promises are resolved in the output, as I told you, is an array of resolved promises.
So we have promised one promise to promise three.
Cool.
But what what will happen if I reject one of them here?
You know, OK, save it, refresh F5 or hit here, spending Arakwal in one of the promise and I'm catching
that everything I promise to where we have that promise rejected, now it reject immediately upon any
of the input promises we rejected and where or where there is a throne in there and it will reject with
the first rejection message or the error that we just got.
So the problem is that this is something that you shouldn't need.
You should know if we have any rejection here, it will stop of executing and it will throw it immediately.
So that is all that you need to know about promise.
Oh, now I have another one interesting promise race.
And actually here I'm going to keep it interesting, same as this one.
We're going to make a couple of changes here, one, two and three right now.
We also need to change this promise, that race.
So first, let me write it down and I'm going to explain what will happen here.
Now, let's go back here and see now.
Promise one.
It's equal to the new promise.
We will resolve them.
Let's put here a hundred and say promise one.
The promise you will be five hundred
in the promise three will be again here is not going to be rejected.
Actually make sure it's resolved.
And the promise three will be how much.
Let's put the promise three or fifty.
So that is half of the first one and we saying it to resolve.
You now promise that race here in passing the year.
But I need to promise one promise
misto and I have the promise.
Three, Don't forget to change this because we have a different conference here.
Right.
And I have then data Konza lock that data or whatever you want.
So what do you think is going to be what is going to happen?
What is the expected output?
OK.
Let's refresh let's face this one here, hit enter, promised more spending and promise three.
Well, why is this is happening?
Why I don't go from the first, second and third promise three has been resolved, but it's 50 milliseconds
right now.
What is a promise that race.
So this is actually a race between three promises to the race method returns.
A promise that fulfills or rejects doesn't matter.
As soon as one of the promises of this iterable here, Arae fulfills or rejects.
So with the value, actually, with the value of this, with the value of the reason for from that promise.
Nice.
So this returns a promise that rejects or Fairfield's as soon as one of the promises here from this
iterable fulfills or rejects.
And I'm just pointing out the value.
OK, what you think will happen if I put it here, reject, would you think that the first one will
go first?
Let's see.
Save it.
Clearing the east and enter.
No I haven't got er undefined identifier promise.
One has already been declared.
Cool refresh again.
Pasted down here.
Nice because I have to refresh and I this new cause that needs to be fresh.
Right.
So Kirkorian one of the promises above promised three.
Very good.
But this is the first promise that rejected.
Right.
So it doesn't matter if it's going to reject or fulfill.
That is the one the first going to raise.
So that's that's going to be actually the winner of this race.
Nice.
So we have promises o promise.
We get a raise and we have promised results.
Now, promise resolve is OK, actually.
Sorry, promise project.
We're going to promise project.
It's a method to return a promise object that is rejected with a given reason.
Right.
So the static promise reject function returns a promise that is rejected.
This is good for debugging or catching an error.
Selective error.
So it is useful to make a reasons of an instance of an error.
Nice.
So here I'm going to say promis reject in.
You will see what I try what I did say like a couple of seconds ago.
So promise we need to call the class reject in here.
I'm going to throw a new error and the description goes here, feel nice, I'm going to use that method
and I can pass a function here.
Right.
So this function will not take any parameters.
But what will happen?
This bar will never be called.
Right.
I'm going to add a function, another comma here.
I'm going to add another function.
And actually here I'm going to catch the error.
What's happening?
Why so cancel the error.
Actually, we're going to say this is promise.
Reject it.
I'm going to see the error note.
So when we have a then method attached with the reject, that method will never go and be executed.
It will go straight to the rest of the code right here.
We are throwing an error.
So the promised reject method returns a promise that is rejected with the given reason.
It was the reason you just throwing up an error.
This is good for debugging purposes or for error, catching more for anything that you want to test
it out.
Let's copy it here.
Based it down there.
Hit enter.
Here it is.
This is the promised reject error and field comes from the value past into this error.
Nice.
So every time we use reject, we know that the method will return a promise object that is rejected
for a given reason.
I'm going to copy the same thing same and I'm going to based it down here.
It's going to say promise is resolved.
Now this is the opposite.
We need to use the syntax promise that resolve cool in here instead of error.
You can print out our success message.
Now we have the then function where it will take a value here and we will cancel the value.
What you think we'll print here, the function error.
Actually, this will never execute.
Because this is totally opposite than this one, when we have a dinner, everything that is there is
going to be executed.
Now the problem is resolved.
Again, going to return a promise object, but it's going to be resolved with the given value.
And the value is here.
We passing here in capacity.
They can pass value, whatever it is.
Now, if there is a dead method, it will go there.
Right.
OK, no, copy it here.
Based on our success, again, the promised state is fulfilled and the results of the undefined right
now, it will never go to this one.
This is a promise project error.
Actually, we don't need to see a promised project because we never said we're going to go there now.
That's it.
So, you know about all the promises.
So we have a class for the promises and we can use one, two, three, four methods.
Right.
So this actually takes as an argument here on the array of promises.
And as soon as they're fulfilled or rejected, it will go through here.
So if they reject it, it will be stopped.
We don't we're not going to have as a result at the end, an array of results, promises, promises
that race.
So we are just seeing here, OK, we have three promises, regardless of it's rejected or resolved.
The one with these fasters fastest, it will go there.
Right.
And the we do have the promised reject and promised results, which are very simple and easy to understand.
These will always reject.
And we need to give the specify the reason it's good for debugging.
And this will always be successful.
And we are just going to print it out here then always this method that will go here, it will be executed.
Right.
So now if you have something important that you want to be executed, no matter what, she's going to
put it here.
That's it.
So thank you very much.
Next lecture, we're going to learn a lot more things and we are going to introduce new method into
2020 that is important and connected with this.
OK, so I will see you in the next one.