Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Problemas para resolver una promesa al obtener subcategorías

En el código a continuación, no estoy undefined como el valor de los subs y no estoy seguro de por qué. Soy relativamente nuevo en el trabajo con Promises y funciones asíncronas, así que estoy seguro de que me estoy perdiendo algo aquí.

 let select = document.querySelector("#category"); async function getMainCategories() { const categoriesURL = "INSERT_URL/categories_main"; return await fetch(categoriesURL) .then((response) => response.json()) .then((categories) => { categories .sort(function (a, b) { var nameA = a.title.toUpperCase(); var nameB = b.title.toUpperCase(); if (nameA < nameB) { return -1; } if (nameA > nameB) { return 1; } return 0; }) .map((cat) => { const subs = Promise.all(getSubcategories(cat.id)); //err - object is not iterable return `<optgroup label="${cat.title}">${subs}</optgroup>`; }); }); } async function getSubcategories(id) { let subcategoriesURL = "INSERT_URL/subcategories"; return await fetch(subcategoriesURL) .then((response) => response.json()) .then((subcategories) => { subcategories .sort(function (a, b) { var nameA = a.title.toUpperCase(); var nameB = b.title.toUpperCase(); if (nameA < nameB) { return -1; } if (nameA > nameB) { return 1; } // names must be equal return 0; }) .filter((subcategory) => subcategory.categories_main_id === id) .map((sub) => { return `<option value="${sub.id}">${sub.title}</option>`; }); }); } async function setHTML() { console.log(await getMainCategories()); // undefined select.innerHTML = "<option disabled selected>Select a category</option>" + (await getMainCategories()); } setHTML();
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!