I am trying to create a NodeJS script that takes JSON data on vehicle makes and models and creates SVG charts, then converts them into PNG images.
I have nested forEach functions where I take each make, process the chart creation for each model before moving to the next iteration of make.
The problem is my script goes through the iterations without finishing processing of all the models, which creates timeouts.
How can I change the following code so that all PNG images for each make get processed and finished before moving to the next make?
const { convertFile } = require('convert-svg-to-png');
const makesJSONs = ['Acura', 'BMW', 'Cadillac'];
makesJSONs.forEach((make) => {
make.forEach((model)=> {
// do stuff to create chart
const run = async ()=>{
// I Want to....
// 1. Render SVG and save to file
const svgStr = await chart.renderToSVGString();
chart.dispose();
fs.writeFileSync(`charts/${model}.svg`, svgStr, (err) => {
if (err) throw err;
console.log(`${model}.svg file has saved`);
});
// 2. Convert SVG to PNG and save to file
const inputFilePath = `charts/${model}.svg`;
const outputFilePath = await convertFile(inputFilePath, {scale: 2, background: '#F7F7ED'});
console.log(outputFilePath);
}
run();
})
})