Actualmente tengo el problema de que mi JS está llegando al final de mis funciones antes de que se hayan completado todas las llamadas a la base de datos. Lo que hace que mi función devuelva un conjunto de datos incompleto.
Ahora probé async/await y Promise.then(). Ahora estoy mezclando ambos pero no consigo que mi función espere hasta que todo esté listo.
Ese es mi código:
async function fillData(dashboards) { let mapDashboards = await new Promise((resolve) => { dashboards.map(async (dashboard) => { if (dashboard.widgets) { await dashboard.widgets.map(async (widget) => { if (widget.panels) { await widget.panels.map(async (panel) => { if (panel.tabs) { await mapPanelTabs(panel).then((tabs) => (panel.tabs = tabs)); } }); } }); } }); console.log(dashboards); resolve(dashboards); }); return mapDashboards; } function mapPanelTabs(panel) { return new Promise((resolve) => { if (panel.tabs) { var tabs = []; panel.tabs.forEach(async (tab, index) => { const dbTab = await Tab.findById(tab.id) .clone() .catch((e) => console.log(e)); if (dbTab) { console.log("dbtab found"); tab = {}; if (dbTab.queryData && dbTab.queryData.id) { await dataController .findById(dbTab.queryData.id) .catch((e) => console.log(e)) .then((queryData) => { if (dbTab.type === "chart") { // Some more logic tabs[index] = tab; }); } } else { tabs[index] = null; } }); resolve(tabs); } }); } Necesito obtener la promesa en fillData para esperar con la resolución hasta que mapPanelTabs con el mapeo.
¿Alguien tiene una idea de cómo hacer que esto funcione?
Gracias por adelantado.