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

97
Views
Length is showing 0 even after using async and await keyword

I Dont know what is going on but even after using async and await keyword still the length is showing zero. Thanks in advance.

const commercial_shoots = [];
  let test;
  React.useEffect(() => {
    async function fetchData() {
      const app_ref = ref(storage, "Home/");
      await listAll(app_ref)
        .then((res) => {
          res.items.forEach((itemRef) => {
            getDownloadURL(itemRef).then((url) => {
              commercial_shoots.push({ img: url });
            });
          });
        })
        .catch((error) => {
          console.log(error);
        });
    }
    fetchData();
  }, []);
  return <div>{commercial_shoots.length}</div>;
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

React only re-renders the component when state or props updates. Here, you are only updating a local variable. So, even when it updates, the UI does not reflect the change.

The solution would be to use commercialShoots as a state in the component.

const CommercialShoots = () => {
  const [commercialShoots, setCommercialShoots] = useState([]);
  useEffect(() => {
    async function fetchData() {
     try {
      const app_ref = ref(storage, "Home/");
      const res = await listAll(app_ref);
      const downloadUrls = await Promise.all(res.items.map(itemRef) => getDownloadURL(itemRef));
      const mappedUrlsToImg = downloadUrls.map((url) => ({ img: url });
      setCommercialShoots(mappedUrlsToImg);
     } catch (error) {
      console.error(error);
     }
    }
    fetchData();
  }, []);
  return <div>{commercial_shoots.length}</div>;
};

NOTE - Since we are using async / await extensively, I took the liberty of updating the .then() to async / await syntax.

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!