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

134
Visualizações
Why is my await using setState before it resolves the promise?

My app keeps crashing the first time it loads because my setState is being fired before waiting for the data from the resolved promise.

Maybe I'm doing something wrong?

const handlePublicationTypeSelect = async (e: React.MouseEvent<HTMLButtonElement>) => {
  setIsLoading(true);
  await getAvailableFiles((e.target as HTMLButtonElement).value);
  console.log(availableFiles);
  reset();
  setValue('publicationType', (e.target as HTMLButtonElement).value);
  setActiveStep((prevStep: ActiveSteps) => prevStep + 1);
  setValue('current', availableFiles[0].id);
  setValue('next', availableFiles[0].id);
};

And then the getAvailableFiles function

const getAvailableFiles = async (type: 'privacy' | 'terms') => {
  const data = await fetch(`/api/publications/manage/documents?type=${type}`);
  const result = await data.json();
  setAvailableFiles(await result);
  setIsLoading(false);
  console.log(availableFiles);
};

First time these run I get an empty array back, but the next time I load this component it fires.

It also does load the data into the availableFiles variable but at first it fires an empty array

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