-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromiseExample.js
More file actions
29 lines (28 loc) · 1.14 KB
/
promiseExample.js
File metadata and controls
29 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// a utilty function to display some data in the ui, nothing related with promises
const displayReposInsideHtmlBody = (repos) => {
const ul = document.getElementById("list"); // where we want to display the repos
const reposToDisplay = repos.slice(0,5); // need only 5 repos
reposToDisplay.forEach(repo => {
const li = document.createElement("LI");
const textnode = document.createTextNode(`name=${repo.full_name}, url=${repo.url}`);
li.appendChild(textnode);
ul.appendChild(li);
});
}
// define a promise
const getGitReposPromise = new Promise((resolve, reject) => {
let request = new XMLHttpRequest;
request.open('GET', 'https://api.github.com/repositories', true);
request.onload = function() {
if(request.status === 200){
const repos = JSON.parse(this.responseText);
resolve(repos)
} else {
reject(request.statusText)
}
}
request.send();
});
// run a promise
getGitReposPromise.then((repos) => { displayReposInsideHtmlBody(repos); })
.catch((error) => { console.log(`error getting repositories, status: ${error}`); })