Hello, in this JavaScript video, I am going to show you promises. So promises are a really cool thing in ESX. And they essentially allow you to take care of asynchronous programming, generally JavaScript is single threaded. So if I type a line here and type another line, line here, it displays. I mean, the empty line doesn't matter, your execute this line, this line, this line, this line, you know, this one after another. But if you're doing something asynchronous, such as a Jeff, when you're, you know, interacting with, you know, the server or the servers or the files code may not, you know, fully execute or a bit of the code fast enough for the next code to be executed afterwards.
So you might actually get some, you know, you know, sync issues. So to handle that you can use stuff like callbacks and Promises, promises, or the new way of doing things. So I'm just going to use a very simple example. But the logic applies whether you're using Ajax or something else asynchronous loading images, but we're just going to create a method to demonstrate it using divide. Text function called divide. And to take two numbers, no, one two.
And we're going to draw promise. So you need to create a promise. And you promise a promise object and this will be a function in itself and your take to resolve. So resolve will be sent back if it's successful, reject if it isn't successful. In here, we are simply gonna say no to not equal zero, then we know that is It can be done, it can be computed. So as a result, No one forced me to.
And I would say, a couple different ways to do this, you can either just put, you know, a quotation mark here. And you could also do that in here, instead of calculating something to just basically returning the message back saying it works. You could even say, no zeros allowed or something along those lines. Or you could enclose this in an error. You can, you know, further stuff in JavaScript as well. This just provides you with a full stack trace.
I'm just going to keep it simple and have no error bars wanted to let you know about that. Okay, last thing, you just need to return the promise. So if we run this, this won't actually display anything in the console or in our do anything good. Could we need to actually call it call it redo device So the name of the function passes some parameters for the static point zero. We do doctor then. And you could have this all on the same line, I'm separating it, you're gonna have quite a few lines of code here, just make a little neater and have a few extra lines as well.
So this is in the middle, open parentheses. And in here, you do function. function, and this is the code. So this is if it's been successful. For the first function, if it's successful. It's supposed to say console dot log, and we'll get the result and you don't actually you can do other stuff here as well.
That's totally up to you. But we are, we're not going to. So now we're going to say, comma and concept and this is 100. Right, so call it or it doesn't really matter. And in here, we're going to say console dot log, or semi colon, we get no zeros allowed to replace this with a two, for example, we should get a result of 2.5. Okay, so now let me show you where the policies really come into play.
So if here, I were to call another promise, it could be the same one, or it could be different one, I'm just going to call the same one. And after this, do the same return, do void fix on the seven and then I need to handle essentially this again. And I do that by putting then basically literally the exact same as this, put that there, but I'm not going to do a number Return, therefore I won't be calling in over promise. And do know for all. All happy with flat. So we get the 2.5 and 0.857.
That's how you get five and zero, we get no zeros allowed and undefined. So if we want to check if this if we want to make sure that this is only one after word and only if it is successful, we can do it like so instead of you know, doing a bunch of complex if an ounce if statements if we're doing you know, running all our lines of code. common example of this is if you're loading images, but you don't know which one's gonna load first, but you generally only want to, you know, use less than the next one will load the next one. If the previous one has loaded, you can do it and like so. That's it. That's what promises are.
This is the general format you can go pretty crazy with it. I want to provide a link though, will give you all of the different little facets and features that promises in JavaScript provided. If you have any questions because it's quite a complex topic, feel free to pop me a method on the potty next programming video.