Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node: How to wait until a file is completed before processing it?

Tags:

file

io

node.js

My Problem is that I can not be sure when a file has been successfully written to and the file has been closed. Consider the following case:

var fs = require('fs');
var outs = fs.createWriteStream('/tmp/file.txt');
var ins = <some input stream>

...
ins.on('data', function(chunk) { out.write(chunk); });
...
ins.on('end', function() {
   outs.end();
   outs.destroySoon();
   fs.stat('/tmp/file.txt', function(err, info) {

      // PROBLEM: Here info.size will not match the actual number of bytes.
      // However if I wait for a few seconds and then call fs.stat the file has been flushed.
   });
});

So, how do I force or otherwise ensure that the file has been properly flushed before accessing it? I need to be sure that the file is there and complete, as my code has to spawn an external process to read and process the file.

like image 200
Teemu Ikonen Avatar asked Feb 01 '13 03:02

Teemu Ikonen


People also ask

How do I wait for NodeJS?

One way to delay execution of a function in NodeJS is to use the seTimeout() function. Just put the code you want to delay in the callback. For example, below is how you can wait 1 second before executing some code.

What is the difference between setImmediate vs nextTick?

nextTick() is used to schedule a callback function to be invoked in the next iteration of the Event Loop. setImmediate() method is used to execute a function right after the current event loop finishes.

What is npm wait on?

wait-on will wait for period of time for a file to stop growing before triggering availability which is good for monitoring files that are being built. Likewise wait-on will wait for period of time for other resources to remain available before triggering success.

What is eventloop in NodeJS?

Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. The event loop allows us to use callbacks and promises.


1 Answers

Do your file's post-processing in the writeable stream's close event:

outs.on('close', function() {
  <<spawn your process>>
});

Also, no need for the destroySoon after the end, they are one and the same.

like image 64
Pero P. Avatar answered Oct 14 '22 16:10

Pero P.