1
1
"use strict" ;
2
2
3
+ const repoUrl = "https://api.github.com/orgs/HackYourFuture/repos?per_page=100" ;
4
+
3
5
function main ( ) {
4
- const repoUrl =
5
- "https://api.github.com/orgs/HackYourFuture/repos?per_page=100" ;
6
- apiCall ( repoUrl , dataHandler ) ;
6
+ apiCall ( repoUrl ) . then ( data => dataHandler ( data ) ) ;
7
7
}
8
8
9
9
function createAndAppend ( name , parent , options = { } ) {
@@ -22,26 +22,33 @@ function createAndAppend(name, parent, options = {}) {
22
22
23
23
// Making a request to the API endpoint:
24
24
25
- function apiCall ( url , callback ) {
26
- let requestIt = new XMLHttpRequest ( ) ;
27
- requestIt . open ( "GET" , url ) ;
28
- requestIt . onload = function ( ) {
29
- if ( requestIt . status < 400 ) {
30
- callback ( requestIt . responseText ) ;
31
- } else {
32
- // If request is not successful, create an error page:
33
- let root = document . getElementById ( "root" ) ;
34
- let errorDiv1 = createAndAppend ( "div" , root , { id : "notfound" } ) ;
35
- let errorDiv2 = createAndAppend ( "div" , errorDiv1 , { id : "notfound1" } ) ;
36
- let errorDiv3 = createAndAppend ( "div" , errorDiv2 , { id : "notfound-404" } ) ;
37
- createAndAppend ( "div" , errorDiv3 ) ;
38
- let errorStat = createAndAppend ( "h1" , errorDiv3 ) ;
39
- errorStat . innerHTML = requestIt . status ;
40
- let errorMsg = createAndAppend ( "h2" , errorDiv2 , { id : "errorMsg" } ) ;
41
- errorMsg . innerHTML = requestIt . statusText ;
42
- }
43
- } ;
44
- requestIt . send ( ) ;
25
+ function apiCall ( url ) {
26
+ return new Promise ( ( resolve , reject ) => {
27
+ let requestIt = new XMLHttpRequest ( ) ;
28
+ requestIt . open ( "GET" , url ) ;
29
+ requestIt . onload = function ( ) {
30
+ if ( requestIt . status < 400 ) {
31
+ resolve ( requestIt . responseText ) ;
32
+ } else {
33
+ // If request is not successful, create an error page:
34
+ let root = document . getElementById ( "root" ) ;
35
+ let errorDiv1 = createAndAppend ( "div" , root , { id : "notfound" } ) ;
36
+ let errorDiv2 = createAndAppend ( "div" , errorDiv1 , { id : "notfound1" } ) ;
37
+ let errorDiv3 = createAndAppend ( "div" , errorDiv2 , {
38
+ id : "notfound-404"
39
+ } ) ;
40
+ createAndAppend ( "div" , errorDiv3 ) ;
41
+ let errorStat = createAndAppend ( "h1" , errorDiv3 ) ;
42
+ errorStat . innerHTML = requestIt . status ;
43
+ console . log ( requestIt . status ) ;
44
+ let errorMsg = createAndAppend ( "h2" , errorDiv2 , { id : "errorMsg" } ) ;
45
+ errorMsg . innerHTML = requestIt . statusText ;
46
+ console . log ( requestIt . statusText ) ;
47
+ reject ( new Error ( requestIt . status , requestIt . statusText ) ) ;
48
+ }
49
+ } ;
50
+ requestIt . send ( ) ;
51
+ } ) ;
45
52
}
46
53
47
54
// Handling the data:
@@ -91,9 +98,15 @@ function dataHandler(data) {
91
98
createAndAppend ( "hr" , rightSide ) ;
92
99
let contributorList = createAndAppend ( "ul" , rightSide ) ;
93
100
contributorList . id = "contributorList" ;
94
- fillCombobox ( dataParsed ) ;
95
- comboChangeListener ( dataParsed ) ;
96
- dataRender ( dataParsed ) ;
101
+
102
+ //Passing parsed values with resolved promises:
103
+
104
+ const fillCombo = Promise . resolve ( dataParsed ) ;
105
+ fillCombo . then ( data => fillCombobox ( data ) ) ;
106
+ const comboChange = Promise . resolve ( dataParsed ) ;
107
+ comboChange . then ( data => comboChangeListener ( data ) ) ;
108
+ const dataRend = Promise . resolve ( dataParsed ) ;
109
+ dataRend . then ( data => dataRender ( data ) ) ;
97
110
}
98
111
99
112
// Filling the combobox:
@@ -173,9 +186,9 @@ function comboChangeListener(obj) {
173
186
date = date . toUTCString ( ) ;
174
187
updateHeadInfo . innerText = date ;
175
188
176
- //Callback contributor url:
189
+ //Passing contributor url data to the function with promise :
177
190
let contrUrl = obj [ key ] . contributors_url ;
178
- apiCall ( contrUrl , getContributors ) ;
191
+ apiCall ( contrUrl ) . then ( data => getContributors ( data ) ) ;
179
192
}
180
193
}
181
194
}
@@ -281,9 +294,9 @@ function dataRender(obj) {
281
294
date = date . toUTCString ( ) ;
282
295
updateHeadInfo . innerText = date ;
283
296
284
- //Callback contributor url:
297
+ //Passing contributor url data to the function with promise :
285
298
let contrUrl = obj [ key ] . contributors_url ;
286
- apiCall ( contrUrl , getContributors ) ;
299
+ apiCall ( contrUrl ) . then ( data => getContributors ( data ) ) ;
287
300
}
288
301
}
289
302
}
0 commit comments