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

98
Views
Client zip multiple fetch

I'm developing a small application that allows the user to select some documents on the frontend and when they click "Download" a zip is being generated with all their documents.

I'm using client-zip for that. It all works fine with 1 document (1 fetch), but there's one small issue, I can't figure out. How can I add dynamic fetches based on the number of elements in an array?

My code so far:

    const allDownloads = [];

    async function downloadTestZip() {
      const code = await fetch(allDownloads[0]);
      const blob = await downloadZip([code]).blob();
      const link = document.createElement('a');

      link.href = URL.createObjectURL(blob);
      link.download = 'test.zip';
      link.click();
      link.remove();    
    }

The allDownloads=[] is filled when the user checks the checkmark next to the document. So there can be up to 200 different docs.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You have to create an array of Promises, and then await them all :

const promises = allDownloads.map( url => fetch(url) ); // or simply .map(fetch), as a shorthand

const codes = await Promise.all(promises);

// now codes is an array of codes (strings, probably?)
about 4 years ago · Juan Pablo Isaza Report

0

To improve latency, you can use an async iterator (client-zip is happy to take that instead of an array).

async function *lazyFetch() {
  for (const url of allDownloads) yield await fetch(url)
}

const blob = await downloadZip(lazyFetch()).blob()

Functionally it does the same thing as the solution with Promise.all (and it's barely more code). However, client-zip will be able to start processing the data as soon as the first fetch Response is ready (instead of all 200 of them).

How does that help ?

Creating the ZIP file involves some computations and copying. So if you let client-zip start early, all that CPU-intensive stuff can happen while the browser is handling the HTTP request for the next file. Browsers typically have a pool of HTTP clients and they won't start 200 requests concurrently.

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!