|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | { |
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')); |
13 | 18 | } |
14 | | - }; |
15 | | - xhr.onerror = () => cb(new Error('Network request failed')); |
16 | | - xhr.send(); |
| 19 | + xhr.send(); |
| 20 | + }); |
17 | 21 | } |
18 | 22 |
|
19 | 23 | function createAndAppend(name, parent, options = {}) { |
|
42 | 46 |
|
43 | 47 | function getRepoDataFromOrgAndAddToDOM() { |
44 | 48 | 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); |
55 | 55 | }) |
56 | 56 | } |
57 | 57 |
|
|
67 | 67 | function getContributors(data) { |
68 | 68 | const contributorsDiv = document.getElementById('contributors'); |
69 | 69 | 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 | + }) |
80 | 76 | }) |
81 | 77 | } |
82 | 78 |
|
|
0 commit comments