From the course: Learning ECMAScript 6+ (ES6+)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Returning promises with fetch

Returning promises with fetch - JavaScript Tutorial

From the course: Learning ECMAScript 6+ (ES6+)

Start my 1-month free trial

Returning promises with fetch

- [Instructor] Remember our spacePeople function from the previous video that went and got some data from an API? It works pretty well but we're going to reformat this request and instead, use something called fetch. Fetch is a function that works natively in the browser. There are also packages that work with Node like node-fetch and isomorphic-fetch so you can use something similar in that environment as well. Fetch is just going to take in the URL of our API. So that api.open-notify.org/astros/json, that's where all of our data is. So we're going to pass that directly into this fetch function. Then we're going to chain on .then and we're going to console.log whatever the response is. Now, fetch returns the entire response object. And the object has a JSON function that parses the body as JSON. So let's go ahead and take the response and we'll call it response.json. Then we're going to console.log everything.…

Contents