0

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

0

2 Answers 2

2

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}`);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! @Matt, this solution is very easy and suitable to my case
1

You would have to really contort this to use async/await, although you could easily convert it to something that you could await somewhere else:

const text = myParsedJSON.map(({ ItemChild }) => {
    return ItemChild
        .map(({ Category, Inputs }) => {
            return Category + '\n' + Inputs
                .map(({ InputName, InputTitle }) => {
                    return InputName + ' - ' + InputTitle;
                })
                .join('\n');
        })
        .join('\n');
});

const awaitableResult = new Promise((resolve, reject) => {
    fs.appendFile(myFile, text, (err) => {
        if (err) reject(err);
        resolve(true);
    });
});

You can now await the awaitableResult in any async function:

async someFunction() {
    await awaitableResult; // file IO done.
    // do whatever needs to happen next.
}

Note that we do all of the data processing before we start writing to the file: writing to disk is s-l-o-w, you want to do it as little as possible. If you really want to stay with the piecemeal approach, use fs.writeStream.

1 Comment

Thanks @Jared Smith, your answer is very clear and usefull for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.