0

i use a script to generate critical css for some wbsites. I loop through an Object of Websites, in the loop i start a async function to generate criticalcss for some pages inside the acutal wbsite. Now i want, that my loop waits for the end of the asyncrones calls.

At the moment i am stuck.

Greetings

website.pages.forEach((page) => {

    let outPutFile = path + "/" + page.name + ".css";

    const file = fs.createWriteStream(outPutFile, { flags: 'w' });

    if(!fs.existsSync(path))
        fs.mkdirSync(path);

    async function startNewJob() {
        const cssFile = page.files.pop();

        if(!cssFile)
            return Promise.resolve();

        return penthouse({
            url : page.url,
            css: website.path + cssFile,
            puppeteer : { getBrowser : () => browserPromise},
            timeout: 60000
        }).then(
            criticalCss =>{
                file.write(criticalCss.replace(/\.\.\//g,'/' ));
                return startNewJob();
            }
        ).catch(e =>
            {
                console.log(e);
            }
        )
    }

    Promise.all([
        startNewJob(),
        startNewJob(),
        startNewJob(),
        startNewJob(),
        startNewJob(),
        startNewJob(),
        startNewJob()
    ])
        .then(() => {
            console.log(page.name + ' done!')
            file.end();
        });

});
  • [Here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) is a reference. Also, see here on [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – drrkmcfrrk Jun 30 '21 at 13:09

0 Answers0