Writing large files with Node.js

后端 未结 7 875
走了就别回头了
走了就别回头了 2020-12-08 06:53

I\'m writing a large file with node.js using a writable stream:

var fs     = require(\'fs\');
var stream = fs.createWriteStream(\'someFile.txt\', { flags : \         


        
7条回答
  •  孤城傲影
    2020-12-08 07:32

    [Edit] The updated Node.js writable.write(...) API docs say:

    [The] return value is strictly advisory. You MAY continue to write, even if it returns false. However, writes will be buffered in memory, so it is best not to do this excessively. Instead, wait for the drain event before writing more data.

    [Original] From the stream.write(...) documentation (emphasis mine):

    Returns true if the string has been flushed to the kernel buffer. Returns false to indicate that the kernel buffer is full, and the data will be sent out in the future.

    I interpret this to mean that the "write" function returns true if the given string was immediately written to the underlying OS buffer or false if it was not yet written but will be written by the write function (e.g. was presumably buffered for you by the WriteStream) so that you do not have to call "write" again.

提交回复
热议问题