简体   繁体   English

如何从数组异步写入 JSON 文件?

[英]How to asynchronously write to a JSON file from an array?

I'm scraping some data and pushing it to any array from which it is written to a JSON file.我正在抓取一些数据并将其推送到将其写入 JSON 文件的任何数组。 Currently it writes to the JSON file after every push to th array...目前它在每次推送到数组后写入 JSON 文件......

    const myArray = [];
    const htmlString = await rp(url);
    $('a','#atozindex', htmlString).map(async (index, element) => {
        let link = $(element).attr('href');
        if (link) {
            //Do something
            myArray.push({ item1, item2, item3 });
            fs.writeFile("./arrayContents.json", JSON.stringify(myArray), function(err) {
                if(err) {
                    console.log(err);
                }
                console.log("Some Message");
            });
        }
    });
    return myArray;

I want to have it push only once at the very end when all the data is already in the array.当所有数据都已经在数组中时,我希望它在最后只推送一次。 So something like this:所以像这样:

    const myArray = [];
    const htmlString = await rp(url);
    $('a','#atozindex', htmlString).map(async (index, element) => {
        let link = $(element).attr('href');
        if (link) {
            //Do something
            myArray.push({ item1, item2, item3 });
        }
    });
    fs.writeFile("./arrayContents.json", JSON.stringify(myArray), function(err) {
        if(err) {
            console.log(err);
        }
        console.log("Some Message");
    return myArray;

But I'm having some trouble figuring it out.但是我在弄清楚它时遇到了一些麻烦。

i think that you can do that like this我认为你可以这样做

$.when(mapping()).then(function( ) { // mapping is a function for your map and until it finish then do that ...

});

OR或者

$.when(mapping()).done(function( ) { // mapping is a function for your map and until it finish then do that ...

    });

until map finish then or done do somting , thant's it直到地图完成然后或完成做 somting ,就这样

you can see learn more about when/then or done here您可以在此处查看有关何时/然后或完成的更多信息

My async/await is rusty, to be sure to test... but I think you have 2 options - await all the asyncs:我的异步/等待很生疏,一定要测试......但我认为你有两个选择 - 等待所有异步:

const myArray = [];
const htmlString = await rp(url);
$('a','#atozindex', htmlString).map(async (index, element) => {
    let link = $(element).attr('href');
    if (link) {
        //Do something
        myArray.push({ item1, item2, item3 });
    }
}).forEach(pending => await pending);
fs.writeFile("./arrayContents.json", JSON.stringify(myArray), function(err) {
    if(err) {
        console.log(err);
    }
    console.log("Some Message");
return myArray;

OR, remove the async:或者,删除异步:

const myArray = [];
const htmlString = await rp(url);
$('a','#atozindex', htmlString).map((index, element) => {
    let link = $(element).attr('href');
    if (link) {
        //Do something
        myArray.push({ item1, item2, item3 });
    }
});
fs.writeFile("./arrayContents.json", JSON.stringify(myArray), function(err) {
    if(err) {
        console.log(err);
    }
    console.log("Some Message");
return myArray;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM