<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.29/browser.js"></script>
<script type="text/babel">
const spacePeople = () => {
return new Promise((resolves, rejects) => {
const api = 'http://api.open-notify.org/astros.json';
const request = new XMLHttpRequest();
request.open('GET', api);
request.onload = () => {
if (request.status === 200) {
resolves(JSON.parse(request.response));
} else {
rejects(Error(request.statusText));
}
};
request.onerror = err => rejects(err);
request.send();
});
};
spacePeople().then(
spaceData => console.log(spaceData),
err => Console.error(new Error('Cannot load space people')
)
);
</script>
<title>Building Promises</title>
</head>
<body>
</body>
</html>