createreadstream 是异步的吗? [英] Is createreadstream asynchronous?

查看:74
本文介绍了createreadstream 是异步的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options .我也有一个普遍的问题.

https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options . I also have a general question .

我可以假设除非文档中另有说明,否则提到的任何函数都是异步的吗?

Can I assume that unless otherwise stated in the documentation , any function mentioned is asynchronous?

推荐答案

createreadstream 是异步的吗?

Is createreadstream asynchronous?

是和否.这个问题实际上更多的是语义问题,因为它在同步外观的界面下隐藏了异步操作.fs.createReadStream() 似乎有一个同步接口.它不会返回承诺或接受回调以在完成后进行通信或发回一些结果.因此,它从界面上看起来是同步的.而且,我们通过使用它知道您无需等待即可开始使用流.因此,您可以像使用同步接口一样使用它.

Yes and no. This question is really more a matter of semantics than anything because it hides an asynchronous operation under a synchronous looking interface. fs.createReadStream() appears to have a synchronous interface. It does not return a promise or accept a callback to communicate back when it's done or to send back some results. So, it appears synchronous from the interface. And, we know from using it that there's nothing you have to wait for in order to start using the stream. So, you use it as if it was a synchronous interface.

这是 fs.createReadStream() 的签名:

fs.createReadStream(path[, options])

而且,在选项对象中,没有回调选项,也没有提到返回的承诺.这不是典型的异步接口.

And, in the options object, there is no callback option and no mention of a returned promise. This is not a typical asynchronous interface.

另一方面,如果您查看 fs.rename() 的签名:

On the other hand if you look at the signature of fs.rename():

fs.rename(oldPath, newPath, callback)

您会看到它需要一个回调,在文档中称为完成回调".这个函数显然是异步的.

You see that it takes a callback that is referred to in the doc as the "completion callback". This function is clearly asynchronous.

但是,fs.createReadStream() 确实打开了一个文件,并且它会异步打开该文件而不会阻塞.

But, fs.createReadStream() does open a file and it opens that file asynchronously without blocking.

如果您想知道 fs.createReadStream() 如何在必须异步打开文件时实现同步,那是因为 fs.createReadStream() 还没有返回时打开文件.

If you're wondering how fs.createReadStream() can be synchronous when it has to open a file asynchronously, that's because fs.createReadStream() has not yet opened the file when it returns.

在流的正常使用中,您可以立即开始从流中读取.但是,在流内部,如果文件尚未打开,它将等到文件完成打开,然后才实际尝试从中读取.因此,打开文件的过程对流的用户是隐藏的(这通常是一件好事).

In normal use of the stream, you can just start reading from the stream immediately. But, internally to the stream, if the file is not yet opened, it will wait until the file is done being opened before actually attempting to read from it. So, the process of opening the file is being hidden from the user of the stream (which is generally a good thing).

如果您想知道文件实际何时被打开,流上有一个 open 事件.而且,如果打开文件时出现错误,流上将有一个 error 事件.所以,如果你想获得技术,你可以说 fs.readStream() 实际上是一个异步操作,异步操作的完成是通过 open 或 <代码>错误事件.

If you wanted to know when the file as actually done being opened, there is an open event on the stream. And, if there's an error opening the file, there will be an error event on the stream. So, if you want to get technical, you can say that fs.readStream() actually is an asynchronous operation and completion of the async operation is communicated via the open or error events.

let rstream = fs.createReadStream("temp.txt");
rstream.on('open', (fd) => {
    // file opened now
});
rstream.on('error', (err) => {
    // error on the stream
});

但是,在 fs.createReadStream() 的正常使用中,程序员不必监视文件打开事件,因为它对用户隐藏并在从下一个读取流时自动处理.当您创建一个读取流并立即要求从中读取(这是一个异步接口)时,流对象在内部等待文件完成打开,然后从中读取一些字节,等待文件读取完成然后通知完成读操作.因此,他们只是将文件打开完成与第一次读取结合起来,为程序员节省了在发出第一次读取操作之前等待文件打开完成的额外步骤.

But, in normal usage of fs.createReadStream(), the programmer does not have to monitor the file open event because it is hidden from the user and handled automatically when the stream is read from next. When you create a read stream and immediately ask to read from it (which is an asynchronous interface), internally the stream object waits for the file to finish opening, then reads some bytes form it, waits for the file read to finish and then notifies completion of the read operation. So, they just combine the file open completion with the first read, saving the programmer an extra step of waiting for the file open to finish before issuing their first read operation.

因此,从技术上讲,fs.createReadStream() 是一个具有完成事件的异步操作.但是,由于它与从文件中读取相结合的方式,您通常不必像异步那样使用它,因为它的异步行为与从文件中的异步读取相结合.

So, technically fs.createReadStream() is an asynchronous operation that has completion events. But, because of the way it's been combined with reading from the file, you don't generally have to use it like it's asynchronous because it's asynchronous behavior is combined with the async reading from the file.

这篇关于createreadstream 是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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