Skip to content

Commit 5439fb1

Browse files
committed
Changed all to promises/then
1 parent b447be9 commit 5439fb1

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

homework/index.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
'use strict';
22

33
{
4-
function fetchJSON(url, cb) {
5-
const xhr = new XMLHttpRequest();
6-
xhr.open('GET', url);
7-
xhr.responseType = 'json';
8-
xhr.onload = () => {
9-
if (xhr.status < 400) {
10-
cb(null, xhr.response);
11-
} else {
12-
cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
4+
function fetchJSON(url) {
5+
return new Promise((resolve, reject) => {
6+
let xhr = new XMLHttpRequest();
7+
xhr.open('GET', url);
8+
xhr.responseType = 'json';
9+
xhr.onload = function () {
10+
if (xhr.status < 400) {
11+
resolve(xhr.response);
12+
} else {
13+
reject(Error(xhr.status));
14+
}
15+
};
16+
xhr.onerror = () => {
17+
reject(Error('Network request failed'));
1318
}
14-
};
15-
xhr.onerror = () => cb(new Error('Network request failed'));
16-
xhr.send();
19+
xhr.send();
20+
});
1721
}
1822

1923
function createAndAppend(name, parent, options = {}) {
@@ -42,16 +46,12 @@
4246

4347
function getRepoDataFromOrgAndAddToDOM() {
4448
const REPOS_URL = 'https://api.github.com/orgs/foocoding/repos?per_page=100';
45-
fetchJSON(REPOS_URL, (err, arrayOfRepoData) => {
46-
if (err) {
47-
alert(err.message);
48-
} else {
49-
arrayOfRepoData.sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : -1);
50-
arrayOfRepoData.forEach(repoDataObj => {
51-
createAndAppend('option', document.getElementById('repo-select'), { text: repoDataObj.name })
52-
})
53-
addListenerOnSelect(arrayOfRepoData);
54-
}
49+
fetchJSON(REPOS_URL).then(function (response) {
50+
response.sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : -1);
51+
response.forEach(repoDataObj => {
52+
createAndAppend('option', document.getElementById('repo-select'), { text: repoDataObj.name })
53+
})
54+
addListenerOnSelect(response);
5555
})
5656
}
5757

@@ -67,16 +67,12 @@
6767
function getContributors(data) {
6868
const contributorsDiv = document.getElementById('contributors');
6969
contributorsDiv.innerHTML = 'Contributors:';
70-
fetchJSON(data.contributors_url, (err, arrayOfContributorData) => {
71-
if (err) {
72-
alert(err.message);
73-
} else {
74-
arrayOfContributorData.forEach(repoObj => {
75-
let key = repoObj.login;
76-
createAndAppend('div', contributorsDiv, { id: key });
77-
createAndAppend('a', document.getElementById(key), { href: repoObj.html_url, target: "_blank", text: key });
78-
});
79-
}
70+
fetchJSON(data.contributors_url).then(function (response) {
71+
response.forEach(repoObj => {
72+
let key = repoObj.login;
73+
createAndAppend('div', contributorsDiv, { id: key });
74+
createAndAppend('a', document.getElementById(key), { href: repoObj.html_url, target: "_blank", text: key });
75+
})
8076
})
8177
}
8278

0 commit comments

Comments
 (0)