Thank you very much for your detailed and helpful answer! I really appreciate the modern async/await approach you shared — it definitely improves readability, especially for more complex asynchronous logic. For now, I’ve decided to stick with my current Promise-based version, as it's a bit simpler for this use case and fits better with the rest of my codebase, which mostly uses .then() chains. Still, I fully agree that async/await is the cleaner and more scalable solution, and I’ll likely switch to it as the project grows. Thanks again for your guidance — it was very insightful! Here’s the version I’m currently using:
function fillTable(tablehead, tablebody, url) {
return fetch(url)
.then(res => res.json())
.then(data => {
if (!Array.isArray(data) || data.length === 0) return;
let fejlec = '<tr>';
for (let key in data[0]) {
fejlec += `<th>${key}</th>`;
}
fejlec += '</tr>';
let sorok = '';
data.forEach(sor => {
sorok += '<tr>';
for (let key in sor) {
sorok += `<td>${sor[key]}</td>`;
}
sorok += '</tr>';
});
$(`#${tablehead}`).html(fejlec);
$(`#${tablebody}`).html(sorok);
})
.catch(error => {
console.error('Error loading table:', error);
});
}