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();
});
});