Q
Q
qwerty2018-02-09 13:15:55
JavaScript
qwerty, 2018-02-09 13:15:55

How to determine that writing data to a file is finished?

After the end of writing data to the file, you need to call the callback.
How to determine that writing to a file is over if the appendfile function is called N times (a lot of data)

function saveData(data, callback) {
    fs.open('info.json', 'a', (err, fd) => {
        if (err) throw err;
        console.log(fd + 'append');
        fs.appendFile(fd, data, 'utf8', (err) => {
            if (err) throw err;
            fs.close(fd, (err) => {
                if (err) throw err;
                console.log(fd + 'close');
            });
        });
    });
}

<Buffer 7b 22 72 65 73 70 6f 6e 73 65 22 3a 7b 22 63 6f 75 6e 74 22 3a 33 32 32 36 37 31 2c 22 69 74 65 6d 73 22 3a 5b 7b 22 69 64 22 3a 35 2c 22 66 69 72 73 ... >
<Buffer 80 d0 b3 d0 b5 d0 b9 22 2c 22 6c 61 73 74 5f 6e 61 6d 65 22 3a 22 d0 90 d0 bb d0 b5 d0 ba d1 81 d0 b0 d0 bd d0 b4 d1 80 d0 be d0 b2 d0 b8 d1 87 22 2c ... >
13append
14append
13close
14close

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2018-02-09
@RidgeA

until the data runs out...
In general, if there is a lot of data, I advise you to use streams (fs.createWriteStream - https://nodejs.org/api/fs.html#fs_fs_createwritest...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question