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

131
Views
Getting fromData after async for loop

I am using two function two handle multiple files upload in ReactJS.

const doSomethingAsync = (file, formD)=>{
    return new Promise((resolve) =>{
      
    const type = file.type.split("/")[0];
    const filename = file.name;
    // file reader to preview file
    let reader = new FileReader();
    reader.onload = () => {
      formD.append("fileList", file, filename);
      setSelectedFileForUpload((prevState) => [
        ...prevState,
        {
          file: reader.result,
          type: type,
          name: filename,
        },
      ]);
    };
    reader.readAsDataURL(file);
    });
    
  }

  const handleFileUpload = (e) => {
    const files = e.target.files;
    if (!files) return false;
    let formD = new FormData();
    console.log("START");
    for (const file of files) {
      console.log("MID");
      doSomethingAsync(file,formD);
    }
    console.log("END");
    for (const file of formD) {
      console.log(file); // not getting file. Don't know why.
    }
  };

If I use want to upload two files (or one file). Then
OUTPUT (if we upload two files) :

START
MID
MID
END

As you can see I am looping through formData by using :

for (const file of formD) {
      console.log(file); // not getting file. Don't know why.
    }

But it is NOT executing (may be because of length is 0). So I want to get formData after for of loop completed.

about 4 years ago · Juan Pablo Isaza
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!