Asynchronous
Callback
<h1>Callback Async - AJAX - List User</h1>
<main></main>
<script>
let users = [];
function displayUser() {
const tables = [];
tables.push(`
<tr>
<th>No</th>
<th>Nama</th>
<th>Umur</th>
</tr>
`);
let no = 1;
for (user of users) {
tables.push(`
<tr>
<td>${no}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
`);
no++;
}
const html = `
<table>
${tables.join("")}
</table>
`
const parent = document.getElementsByTagName("main")[0];
parent.innerHTML = "";
parent.insertAdjacentHTML("beforeend", html);
}
function list() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
users = JSON.parse(this.responseText);
};
xhttp.open("GET", "https://node-rest.rijalasepnugroho.com/users");
xhttp.send();
}
list();
displayUser();
</script>Promise
Promise.all()
async/await
Last updated