Node: fs write() doesn't write inside loop. Why not?

后端 未结 3 1063
盖世英雄少女心
盖世英雄少女心 2020-12-09 21:39

I want to create a write stream and write to it as my data comes in. However, I am able to create the file but nothing is written to it. Eventually, the process runs out of

3条回答
  •  眼角桃花
    2020-12-09 22:29

    The problem is that you aren't ever giving it a chance to drain the buffer. Eventually this buffer gets full and you run out of memory.

    WriteStream.write returns a boolean value indicating if the data was successfully written to disk. If the data was not successfully written, you should wait for the drain event, which indicates the buffer has been drained.

    Here's one way of writing your code which utilizes the return value of write and the drain event:

    'use strict'
    
    var fs = require('fs');
    var wstream = fs.createWriteStream('myOutput.txt');
    
    function writeToStream(i) {
      for (; i < 10000000000; i++) {
        if (!wstream.write(i + '\n')) {
          // Wait for it to drain then start writing data from where we left off
          wstream.once('drain', function() {
            writeToStream(i + 1);
          });
          return;
        }
      }
      console.log('End!')
      wstream.end();
    }
    
    writeToStream(0);
    

提交回复
热议问题