From the course: Learning ECMAScript 6+ (ES6+)
Unlock the full course today
Join today to access over 22,700 courses taught by industry experts or purchase this course individually.
Loading remote data with promises - JavaScript Tutorial
From the course: Learning ECMAScript 6+ (ES6+)
Loading remote data with promises
- [Instructor] We often use promises to load data. Let's create a function called spacePeople that returns a promise and we'll call it spacePeople because it's going to fetch some data from an API of astronauts who are currently in space. So spacePeople again will return a new Promise. It's going to have resolve and rejects as the arguments here. We also are going to create a link to an API. So we'll use this open-notify astronaut API. And then we need to build a request. So we're going to create another variable called request. This is going to be equal to a new XML HTTP request. Now, when we open this request, we're going to send it as a GET request. In other words, we're going to ask for some data from this API. When the request loads, we're going to set it to this function. So if the status is 200, meaning everything is going well, so we'll just parse the response here on line nine. Otherwise, in other words, if the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.