Here's the code:
but.addEventListener("click", () => {
fetch("employees.json")
.then(resp => {
if (res.status >= 400) {
return Promise.reject();
}
console.log(resp);
return resp.json();
// const a = resp.json();
// for (const worker of a) {
// const employee = AddDOMWorker(worker);
// res.appendChild(employee);
// }
})
.then(
(emps) => {
for (const worker of emps) {
const employee = AddDOMWorker(worker);
res.appendChild(employee);
}
}
)
});
function AddDOMWorker(employee) {
const div = document.createElement("div");
div.classList.add("friend");
div.textContent = `${employee.id} ${employee.position}`;
return div;
}
Why I should use .then after returning resp.json(). Why can't I just put resp.json() in a variable and get rid of the second .then (commented code)?