1

Before opening the stream need to check whether the stream is already opened or not

var fs=require('fs');
var stream=fs.createWriteStream(filePath, {flags: 'a'});
stream.on('open',function(){


});
4

1 回答 1

0

流将open在打开时发出,因为这是异步的,无法判断它何时发出,您甚至可以编写类似的东西

var _path = "text.txt";
var stream1 = fs.createWriteStream(_path,{flags:"a"});
var stream2 = fs.createWriteStream(_path,{flags:"a"});
var stream3 = fs.createWriteStream(_path,{flags:"a"});
var streamN = fs.createWriteStream(_path,{flags:"a"});

open当他们准备好时,所有人都会发出事件。

如果您console.log在发出 open 时使用其中一个对象进行转储,您将看到一个具有许多属性的对象,例如_writableState保留写入状态的位置。

一种不太棒的方法是尝试下面的方法

var _path = "text.txt";
var stream = fs.createWriteStream(_path,{flags:"a"});
var stream2 = fs.createWriteStream(_path,{flags:"a"});

var lock = false; // try switching this , when opened and on other events to get the flow.
于 2013-09-19T15:00:06.157 回答