hope you all are having a good day.
I try to make fillClassData() wait for createClassElementsWith(). I figured I have to use async await and return a promise, data somehow to make it work. Any ideas how to solve this in a clean readable way? What would I return?
Code:
// get data
async function fillClassData() {
const prop_id = prop_select.children('option:selected').val();
const selected_class = config.getClass();
// fetch data
const data = await api.fetchClass(prop_id);
// create options
await createClassElementsWith(data);
if (itemsExist(selected_class)) {
class_select.val(selected_class);
// fill table info
fillTableData();
}
}
// creates options for classes
async function createClassElementsWith(data) {
// clear options
class_select.find('option').remove();
// create new options per entry
if (data.length <= 0) {
generateOption('default', 'No options yet', class_select);
} else {
generateOption('default', 'Select Class...', class_select);
for (let item of data) {
generateOption(item.class_id, item.class_name, class_select);
}
}
class_select.children('option')[0].selected = true;
}
// creates element with given parameters
function generateOption(value, text, select) {
// create element
let opt = document.createElement('option');
opt.value = value;
opt.innerHTML = text;
// set some options to disabled
if(value === 'default') opt.setAttribute('disabled', true);
// append to select
select.append(opt);
}
data example:
class_id: "3179807"
class_longname: "long class name"
class_name: "short class name"
Btw. the function fillClassData() is called based on a select option of a prop. The function fillTableData() fills in a table with the data gotten based on those two selects.