WEBVTT Understanding promises is pretty cool. And I think it would be cool to show off an example of it in practice so let's say we did want to access an external API. And as you can see you have these Star Wars API page open here which is a pretty nice resource. It's sort of an external API we can practice on to do extra fast requests and see what those look like. So we're going to be using Learn Java Coding From Digital Marketing agencies in Bangalore the fetch API and the Fettes. It's pretty cool. It's basically a way to do things like getting requests to get data back from an external source. You could use it to for example get some tweets from Twitter using Twitter's API. You could use Digital Marketing agencies in Bangalore to get photos Star Wars API. It's very easy to use and it's just kind of fun to access things like the Star Wars character or planet names. So we're gonna use the fetch API for that fetch. It's still technically experimental but it's pretty widely supported in most browsers. As of the time of this recording is now supported in Safari as well as Firefox and Chrome. So you know support is getting there it's pretty good overall. So just be aware that you know it's technically still experimental so might not work in all possible browsers. But that being said let's jump right in and see what happens.


So the fetcher API returns a promise and we know all about promises. And just to show what it looks like. Basically, you just use the keyword fetch and you give it a URL to use in quotes here. So you know if you want it to fetch some your L you would put it in here. Let's go ahead and let's fetch the Star Wars API. And if you look at the Digital Marketing Company you can see the example of Digital Marketing agencies in Bangalore we're just going to paste this in and make sure the format is correct. I mean it makes it yes. And do slash. So we don't need to know too much about how to fetch works but we do need to know that it returns a promise and we know how to handle that right. So if we say get this promise back we're going to get some kind of data back provided we made a correct request. So we're going to get some kind of data to let's console log the data to take a quick peek at it. And just in case we made a mistake we can catch any error and console log that error. Right. So let's try this out Digital Marketing agencies in Bangalore. We'll open up our console. Let's see what happens. So we run it and we got some kind of weird thing back. Now this is something we may not have seen before which is it's a response object and we get a response and we have to basically use a callback to get at the body.


You know this is a TTP type stuff. And again we don't really need to worry about it too much but if we inspect it in the chrome dev tools we can see that it's a response type object and it has some stuff in here that doesn't really make sense. We can see Digital Marketing agencies in Bangalore. OK got back that means is a successful get request essentially in terms of status. You may be familiar with status in terms of TTP. You get that if you try to access a site that doesn't exist for example. And so we need to get from this to the actual body of the requests and we basically need to use a callback that's embedded in the response prototype called Jaison. So Jason will convert it into something that we can actually understand and use and not to get too into the specifics of Jason. It's basically a way to kind of parse data stands for javascript object notation. But if we use that callback built into the prototype of this response object we can invoke it like this doesn't need to take any arguments. And now if the data does Jason recall that we are returning a promise here implicitly we can change another then onto it. Let's grab that data which I'm going to call D. And now this console log that data we got back in our response to get to the body of their response.


So let's see hey this looks a lot more like something that we can actually use. This just transformed the body of the response into a regular javascript object. You can see that because an object is a bunch of fields that we can just directly access. Right now if we inspect this one in the console read it's just a regular object and we can call any object prototype methods on it. We can access the climate we can access the name. This one happens to be tattooing as you see it makes sense for Planet one to be tattooing if you're a star wars fan at all. So that's kind of neat. Right. We are accessing an external API. We're doing kind of a real-world example instead of just console logging over and over it even though we are still cataloging So that being the case you know the fact that we're just getting an object back means that we can do something with that. And by the way, if we did something incorrectly, for example, we misspelled something.


Let's see if our catch gets fired and we get an error. So what's the message error message. An unexpected token doesn't like that we misspelled Planetes there. So that's kind of neat. So that's what an excellent API looks like. Now let's do something cool with it. Let's going to tinker Digital Marketing agencies in Bangalore around with our dorm because we know how to do that at this point. Right. And then we can kind of throw this into a function we can add an event listener.