Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
JavaScript is not waiting for Async Await

I currently have the problem that my JS is reaching the end of my functions before all database calls have fulfilled. Which causes my function to return an incomplete dataset.

I've now tried async/await and Promise.then(). Now i'm mixing both but i don't get my function to wait until everything is done.

That's my code:

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);
    }
  });
}

I need to get the promise in fillData to wait with the resolve until mapPanelTabs is done with the mapping.

Does anyone have an idea how to get this working?

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda