fs.writeFile callback never gets called, same for

2019-07-17 07:23发布

I am writing a small text file (~500B) but, strangely, I get an empty file if I write using asynchronous methods such as fs.writeFile(..) (or WriteableStream's write/end method).

This works:

var scanInfo = getScanInfo( core ); // returns several lines delimited by \r\n
fs.writeFileSync( filename, scanInfo, 'ascii' );

This creates empty file and the callback function never produces any output:

var scanInfo = getScanInfo( core );
scanInfo.push('') ;
scanInfo = scanInfo.join(DOS_CRLF);
fs.writeFile( filename, scanInfo, 'ascii', function ( err ) { 
   if(err) { console.error('Failed'); console.error(err) ; }
   else { console.log('OK'); }
});

I was looking for similar posts but in the one I found the problem was something else (calling another function returning the content) but my content is a text string (verified by debugging).

The similar post: fs.writeFile() doesn't return callback

Platform> Win8.1 x64

NodeJS> x64 0.12.0

P.S. The application using the function that is actually writing the file was written in a "plain nodejs" style using callbacks but as it got more complicated I rewrote the main processing stream using Q and Q-IO. So now the processing starts like this:

(in the main module)

var qfs = require('q-io/fs') ;
...
qfs.read( configFile )
.then( doSomeConfig )
.then( function( config ) { 
    var promise = qfs.read( config.inputFile, someOptions );
    return promise ;
})
.then( processMyInputData /* (binaryData) returns {Core} */ )
.then( writeMyOutputData  /* (core)   returns {undefined} */  )
.fail( reportSomeErrors   /* (reason) returns {undefined} */ )
.done( reportFinished ) ;

The point is that in the main stream the fail function never reports any problem, either. Function reportFinished() reports that everything was OK and there is no place to throw any exception because the original snippet above, which is a function located in another module and called as part of writeMyOutputData( core ) never gets to call the callback and therefore it is not possible to do any exception throwing or any kind of error processing.

However, after reading Joseph's comment that it works for him I suspect there might be some interference between the standard fs module and q-io/fs

2条回答
放荡不羁爱自由
2楼-- · 2019-07-17 07:43

I had a similar issue with fs.stat The issue was that i was writing a grunt task and the task didn't know it was asynchronous so the synchronous code finished and just terminated the application before the fs.stat callback could be called.

This is probably not your issue, but it might help others.

Making a grunt task can be done like this: Wait async grunt task to finish

查看更多
不美不萌又怎样
3楼-- · 2019-07-17 07:48

OK, after careful deugging problem identified. As Joseph mentioned, not related to fs.writeFile() at all.

In my application there are in fact two file writes running "concurrently". The one listed in my question and another one, writing data progressively as it calculates some averages.

The other, progressively writing function, had a bug (misspelled variable name), causing a Reference Error to be thrown in the course of action (in between successive writes). This exception, for some reason I do not quite understand, did not appear anywhere in the chain. According to Q documentation, Promise.done() should throw any unhandled exceptions, but this was not the case.

After I added several fail() handlers in the promise chain, I was able to locate the bug and achieve reasonable behavior of the whole application.

The error is therefore related to bad programming style (not handling exceptions properly) rather than fs module. However, I can't believe that there could be such thing as unhandled exception that can get lost and never appear in the daylight. Also I can hardly believe that an exception in asynchronous operation B can affect another, non-related asynchronous operation A.

查看更多
登录 后发表回答