0

i have async function in which I write an array and I need to use it further in the code, am I doing the right thing?

js:

var mastersResp = [];
var masterlist = [];

async function getRespMaster() {
  let respons = await fetch('masters.php', {
    method: 'get',
})
  mastersResp = await respons.json();
  masterlist = [
    { name: mastersResp[0]['name'], id: mastersResp[0]['id']}
  ];
  console.log(masterlist[0]);
};

getRespMaster();

const masterSelect = document.getElementById("master-select");
masterlist.forEach(master => {
  const option = document.createElement("option");
  option.value = master.id;
  option.text = master.name;
  masterSelect.appendChild(option);
});

I tried to run the code, but nothing happens. Checking the array via the console inside the function is triggered, and outside its boundaries, the array is output empty. Console.log just send me "undefined". I know that console.log is triggered earlier than my function, but it doesn't give me anything

naffi
  • 13
  • 2

0 Answers0