fs.writeFile回调永远不会被调用,WritableStream.write等相同 [英] fs.writeFile callback never gets called, same for WritableStream.write, etc

查看:1109
本文介绍了fs.writeFile回调永远不会被调用,WritableStream.write等相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小的文本文件(〜500B),但奇怪的是,如果我使用异步方法,如fs.writeFile(..)(或WriteableStream的写/结束方法)写一个空文件。



这适用于:

  var scanInfo = getScanInfo //返回由\r\\\
分隔的多行
fs.writeFileSync(filename,scanInfo,'ascii');

这会创建空文件,回调函数不会生成任何输出:

  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');}
});

我在寻找类似的帖子,但在一个我发现问题是别的(调用另一个函数返回内容),但我的内容是文本字符串(通过调试验证)。



类似的帖子: fs.writeFile()不返回回调



平台> Win8.1 x64



NodeJS> x64 0.12.0



PS使用实际写入文件的函数的应用程序使用回调以plain nodejs风格编写,但是由于它变得更复杂,我使用Q和Q-IO重写了主处理流。
现在处理开始如下:



(在主模块中)

  var qfs = require('q-io / fs'); 
...
qfs.read(configFile)
.then(doSomeConfig)
.then(function(config){
var promise = qfs.read .inputFile,someOptions);
return promise;
})
.then(processMyInputData / *(binaryData)returns {Core} * /)
.then(writeMyOutputData / *核心)返回{undefined} * /)
.fail(reportSomeErrors / *(reason)returns {undefined} * /)
.done(reportFinished);问题是,在主流中,失败函数不会报告任何问题。函数reportFinished()报告一切正常,没有地方可以抛出任何异常,因为上面的原始代码片段,位于另一个模块中,并作为writeMyOutputData(core)的一部分调用的函数永远不会调用回调,因此它是不可能做任何异常抛出或任何种类的错误处理。



但是,在阅读约瑟夫的评论,它的工作原理后,我怀疑可能有一些干扰标准fs模块和q-io / fs

解决方案

正如Joseph所说,与 fs.writeFile()无关。



在我的应用程序中, 。



另一个渐进写入函数有一个错误(拼写错误的变量名称)导致在操作过程中(在连续写入之间)抛出引用错误。这个异常,由于某种原因我不太明白,没有出现在链中的任何地方。根据Q文档,Promise.done()应该抛出任何未处理的异常,但是不是这样。



在promise链中添加了多个fail ,我能够找到错误,并实现整个应用程序的合理行为。



错误因此与坏的编程风格(不正确处理异常)而不是fs模块。然而,我不能相信可能会有这样的事情,如未处理的异常,可以迷路,永远不会出现在白天。此外,我几乎不能相信异步操作 B 中的异常会影响另一个非关联的异步操作 A


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

解决方案

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.

这篇关于fs.writeFile回调永远不会被调用,WritableStream.write等相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆