fetch() is not reliable AFAIK because it is async. I have tried to replace a table row in a reliable way for some days and found this way to replace a table row.
1. Edit record2. fetch the record with delay3. update the table
“`
async function update_sql(url, json) {
let body = get_updatebody(json)
let obj = JSON.parse(json);
let id = obj[mod_id]
await fetch(url, body)
.then(response => (response.json())) //no return values
.then(get_newrow(id));
finish()
tippy2()
}
function get_newrow(id) {
setTimeout(function() {
let url = “https://api3.go4webdev.org/” + mod + “/id/” + id;
fetch(url)
.then(response => response.json())
.then(newdata => replace_tablerow(newdata))
}, 250);
}
function replace_tablerow(data) {
alert(JSON.stringify(data))
let row = document.querySelector(‘table tr[data-id=”‘ + data[mod_id] + ‘”]’);
for (let key in data) {
let val = data[key];
let cell = row.querySelector(‘td[data-key=”‘ + key + ‘”]’);
if (cell) cell.innerHTML = val;
}
}
My question is: Is there a even more reliable way to edit a record and update the table row with new values?
Theoretically I can edit and send return values, but this only seems to result in `object.Promise
1. Edit record and send return values2. Replace the table row with returned values