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

108
Visualizações
How to wait async data to start sync function

I get some data from an api call and set them in a state. Then I use this state variable in another function to filter some data. When the user opens the interface for the first time the data doesnt show because the sync function gets the empty data from the state.

Here is the code :

const [evQuestion, setEvQuestion] = useState();
const [answers, setAnswers] = useState();

const getEvaluationsQuestionsByOrganizations = async (evalId) => {
    const response = await apiStandarts.get(`/evaluation-questions?organization_evaluation=${evalId}`);
    setEvQuestion(response.data);
  };

  const evAnswers = () => {

    const evAnswers = questions.map(q => {
        return evQuestion?.map(ev => {
          return q.question_options.find(i => i.id === ev.questOptionId)
        });
      });
      const filterAnswers = evAnswers.map(q => {
        return q?.filter(Boolean)
      })
      const answersToObject = filterAnswers.map(item => {
        return convertArrayToObject(item)
      });
      const arr = {...answersToObject}
    const obj2 = Object.fromEntries(
      Object.entries(arr).map(([key, value]) => [key, value])
    )
     const obj3=  Object.values(obj2).map(item => {
        return {[item.question]: {...item}}
      })
      const savedAnswers = convertArrayToObject(obj3);
      console.log(savedAnswers)
      setAnswers(savedAnswers)
    }

useEffect(() => {
 getEvaluationsQuestionsByOrganizations();
 evAnswers();
}, [])

I've tried to wrap the evAnswers function in a settimeout function but with no luck. How can I achieve this, any ideas?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try adding another useEffect hook that depends on evQuestion state.

useEffect(() => {
 getEvaluationsQuestionsByOrganizations();
}, []);

useEffect(() => {
 evAnswers();
}, [evQuestion]);
about 4 years ago · Juan Pablo Isaza Relatório

0

the function getEvaluationsQuestionsByOrganizations(..) is defined as async function, but you are using it synchronously, in that case you should call your codes as below:

useEffect(() => {
 const fetchedDataAPI = async () => {
    return await getEvaluationsQuestionsByOrganizations();
 };

 fetchedDataAPI
  .then(res => { evAnswers();})
  .catch(err => {..});
;

}, []);
about 4 years ago · Juan Pablo Isaza Relatório
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