My goal is to "pause" the EventListener function in order to get something done by calling another function (getCategoriesIDs) that returns a value that I will use to continue the EventListener function execution.
When I log the categoriesIDtoNameMapped after the execution it comes out as UNDEFINED.
Would greatly appreciate your help.
form.addEventListener("submit", async (e) => {
//do something
try {
let categoriesIDtoNameMapped = await getCategoriesIDs()
console.log(categoriesIDtoNameMapped)
} catch (err) {
console.log(err)
}
//do something with the categoriesIDtoNameMapped
}
function getCategoriesIDs() {
fetch('http://localhost:1337/api/categories', {
method: 'GET',
headers: {
'Content-type': 'application/json',
Authorization: `Bearer ${JWT_TOKEN}`
}
})
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((response) => {
const categoriesIDtoNameMapped = {}
for (let i = 0; i < response.data.length; i++) {
categoriesIDtoNameMapped[response.data[i].id] = response.data[i].attributes.name
}
return new Promise(resolve => {
resolve(categoriesIDtoNameMapped)
});
});
}