Sometimes it happened that you didn’t get any response And at that time when you wait for a certain amount of time that time is called pending time on once you will not get any response After that amount of time it will be rejected Right So before promises we had callbacks in callbacks you get complicated nested functions right So, for example, this is the element dot On this element we’re adding an event listener.
Click on submit button form Right So when there’s a click submit form will get open right So then inside that servant but informed there is another form So let’s suppose the logging form So that thing the callbacks were much complicated as computer promise So if you Yeah then you can see the example right in the function call You will have If this is a function right in that argument you will have another function called which will have another function called So this was nested functions calls Where There Right So the stack will be filled with all the function calls.
That was all about callback So right now we’re focusing on promises So let’s see what How we can write a promise Basically if you see we have written here a promise right So it’s a constant promise and then we declare it with the help of new keyword new promise Then it will have two arguments Either it will be resolved that is resolved or reject for example Then with the help of arrow function we have put that right now we’re putting that it will be always is all right If you see that truth is written that means always it will be resolved So once we will run this thing we will always get it worked.
If you see the example and if we’re not able to resolve it right let’s put a condition on it Right So let’s suppose if 10 plus 10 is 20 right so we will put if X is equal to 20 Only then it will be work It will work right It will get a resolve message or if it’s not then we’ll get a reject message It broke right So if you see the promise then how we can call this function right with the help of this promise dot Then what happens after this promise is fulfilled What happens We’re basically printing out the result So basically we will get a result something Maybe it worked All right we’ll get fulfilled.
Those 23 states right fulfilled rejected or pending So this result will contain that And along with the message that we got it worked Okay so if it is not then we will write dot catch on We will get that thing right So if you see the promise how we can write it let’s see a demo off it And maybe things will be clear from that okay so now let’s quickly go into the demo So now basically how You’re right to let’s put it again The note pad Right So sublime Coast Right So basically we wrote coast and then we write Promise can be any name right We’re just bringing this name might vary Then new then this thing it has to be the right constructor The promise Right And then it will have two arguments Either it will be resolved right We will have a resolve.
It will have two arguments Then we will write our arrow function Right So this is our arrow function Let me just maximize it a little so that it’s visible Okay so now what we have is a promise Then it is all in a reject Then the arrow function Then what happened If it is true right If this thing is true then you will just resolve You will use resolve And then let’s suppose it worked Any message right We’ll see how this message why these messages are important Okay else right What We will play print in the result else part that reject on Let’s go to it here Eight dead We’ll put a message that it did not work okay that’s it Then we will close our promise here the exit.
After that what will print or print will use Promise the object this object that we created Okay we will use Promise the then If this promise has been fulfilled right then what he will do You will just bring the result That message that you will get from that would bring that result plus some can coordination and then some Ha ha ha Maybe Yeah You know nothing okay so then Ha haha And that’s it okay so now let’s go We based this thing in the control Let’s control plus this thing as well Okay So this is the promise This is how it looks okay so when we press enter then you will see you will get a message.
Let me minimize it a little okay so now when I pressed enter You see You got this message promise than the promise has been fulfilled Right And it worked The message that we got right in here This message Right So here we got this message It worked And then that state that contamination that we did right If we put a space in here then it will look a bit better Okay Sorry I saved this fight So control a controlled Copy Mm Okay This promise was already defined Okay so let me change the name of this promise Promise Let me just go back on then do this Presenter.
Now you see if you see here what we have this promise fulfilled as it worked Hahaha Then it’s a space Okay Great So now let’s clear this screen And now let’s see one more bit variation What happens when this promise is not fulfilled Let’s do one thing let’s remove this thing let’s put it falls in here right Let’s put this SP on Then put a B in here And now let’s see what happens Okay so now we don’t have What happens if this promise has not Bean fulfill what happens We got an error right It says rejected Right The rejected state right It was not fulfilled It didn’t work Right So that was the message right It didn’t work right So now if you want to catch that thing and you want to make it fulfilled just like the other thing Then we just write catch right And then we will put in the result came as the result than the arrow function on Then we print that result.
Let’s copy all this thing and let’s change the name of the promises Well to be one on Que select all copy all Then we based it Now you see even though the problem was not resolved right But we got the message from that We were informed that okay we what We’re whatever we were doing you were not able to do that But he still informed me right The promise was fulfilled But it was a negative promise basically But it was It’s showing Right You see it’s showing that fulfilled right So earlier it was showing the rejected state on Now it showed the fulfilled state.
If there is sometimes maybe there is a chance that sometimes you’re expecting and it takes a lot of time that at that time it will show you the pending message Okay that is all about promises, Okay This is the message that we got right It worked Now let’s see some advance every functions right We will see some everyone We have already seen any functions like push pop Those were some methods Now let’s see some of the different functions that it has So the first one is map So basically it is used to create a copy off the area with some manipulation So basically we will be having some area already Right As you see 123 is an area which has been defined already Now we will manipulate okay with the help of four each that if you see in the example we have area door for each.