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

216
Views
Determine when jszip async function is complete

How can I let spellcheckePub() function know when gatherWords() function is fully done executing? gatherWords() is processing epub books so it takes a while and when I console.log(words) it is empty, presumably because the forEach hasn't completed yet.

masterePub is a zip(epub) loaded with jszip. forEach executes for each file/dir listing in loaded zip file.

 function spellcheckePub() {
      gatherWords();
      console.log(words);
      // do stuff with words
 }

    function gatherWords() {
      words = [];
      masterePub.forEach(function (relativePath, file) {
        if (!file.dir) {
          masterePub.file(file.name).async("text")
          .then(function success(content) {
            //make an array of words
            content = content.split(/\s+/);
            words = words.concat(content);
          });
        }
      });
    } // end spellcheckePub function
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Collect the promises you create in an array, call Promise.all on it:

async function spellcheckePub() {
  const words = await gatherWords();
  console.log(words);
  // do stuff with words
}

async function gatherWords() {
  const promises = [];
  masterePub.forEach((relativePath, file) => {
    if (!file.dir) {
      promises.push(file.async("text").then(content => {
        //make an array of words
        return content.split(/\s+/);
      }));
    }
  });
  const wordArrays = await Promise.all(promises);
  return wordArrays.flat();
}
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!