Created
May 15, 2020 18:40
-
-
Save bakpa79/a3d3e137d391c6bb80029aff16be7d60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Referenced from http://ccoenraets.github.io/es6-tutorial-data/promisify/ | |
let request = obj => { | |
return new Promise((resolve, reject) => { | |
let xhr = new XMLHttpRequest(); | |
xhr.open(obj.method || "GET", obj.url); | |
if (obj.headers) { | |
Object.keys(obj.headers).forEach(key => { | |
xhr.setRequestHeader(key, obj.headers[key]); | |
}); | |
} | |
xhr.onload = () => { | |
if (xhr.status >= 200 && xhr.status < 300) { | |
resolve(xhr.response); | |
} else { | |
reject(xhr.statusText); | |
} | |
}; | |
xhr.onerror = () => reject(xhr.statusText); | |
xhr.send(obj.body); | |
}); | |
}; | |
request({url: "employees.json"}) | |
.then(data => { | |
let employees = JSON.parse(data); | |
let html = ""; | |
employees.forEach(employee => { | |
html += ` | |
<div> | |
<img src='${employee.picture}'/> | |
<div> | |
${employee.firstName} ${employee.lastName} | |
<p>${employee.phone}</p> | |
</div> | |
</div>`; | |
}); | |
document.getElementById("list").innerHTML = html; | |
}) | |
.catch(error => { | |
console.log(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment