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