gpt4 book ai didi

javascript - 查看流 block 是否是最后一个

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:44 25 4
gpt4 key购买 nike

在转换函数中编写 NodeJS 转换流时,我如何知道该 block 是最后一个 block 还是没有任何新 block 。

_transform(chunk: any, encoding: string, callback: Function): void {
// accumulating chunks here to buffer
// so that I need to do some processing on the whole buffer
// and I need to understand when to do that
}

所以我需要知道进入 Stream 的 block 何时结束,对由所有 block 组成的缓冲区进行一些处理,然后将处理后的数据从流中推送。

最佳答案

_transform 中,您无法确定是否会有更多数据。

根据您的用例,您可以监听 end 事件,或使用 _flush:

Stream: transform._flush(callback) :

Custom Transform implementations may implement the transform._flush() method. This will be called when there is no more written data to be consumed, but before the 'end' event is emitted signaling the end of the Readable stream.

Within the transform._flush() implementation, the readable.push() method may be called zero or more times, as appropriate. The callback function must be called when the flush operation is complete.

关于javascript - 查看流 block 是否是最后一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46494727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com