Transform nested .map methods with async/await model

I’m managing a JSON file like this example:

[
    {
        "Item": "Item1",
        "ItemChild": [
            {
                "Category": "Category1",
                "Inputs": [
                    {
                        "InputName": "1",
                        "InputTitle": "Title1",
                    },
                ]
            },
            {
                "Category": "Category2",
                "Inputs": [
                    {
                        "InputName": "2",
                        "InputTitle": "Title2",
                    },
                ]
            }
        ]
    }
]

I’m parsing this file, getting all the Category, InputName and InputTitle keys values and appending them in a file using this logic:

myParsedJSON.map(({ ItemChild }) => {
    ItemChild.map(({ Category, Inputs }) => {
         fs.appendFileSync(myfile, Category);
         Inputs.map(({ InputName, InputTitle }) => {
             fs.appendFileSync(myfile, InputName + ' - ' + InputTitle);
         });
     });
});

I want use an asynchronous version of this logic with async/await model and async operations. What’s the best way to do that? Thanks

Answer

Array functions don’t play nice with async so convert them to for of loops

const fsp = require('fs').promises

for (const { ItemChild } of myParsedJSON) {
    for (const { Category, Inputs } of ItemChild) {
        await fsp.appendFile(myfile, Category);
        for (const { InputName, InputTitle } of Inputs) {
            await fsp.appendFile(myfile, `${InputName} - ${InputTitle}`);
        }
    }
}