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

106
Views
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 answers
Answer question

0

Try adding another useEffect hook that depends on evQuestion state.

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

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

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 Report
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!