Deno.iter(r: Reader, options?: { bufSize: number }) : AsyncIterableIterator<Uint8Array>
将Reader r变成异步迭代器。
Turns a Reader, r, into an async iterator.
r : Reader
options ?: { bufSize: number }
r : Reader
options ?: { bufSize: number }
AsyncIterableIterator<Uint8Array>
AsyncIterableIterator<Uint8Array>
let f = await Deno.open("/etc/passwd");
for await (const chunk of Deno.iter(f)) {
console.log(chunk);
}
f.close();
第二个参数可用于调整缓冲区的大小。 缓冲区的默认大小为32kB。
Second argument can be used to tune size of a buffer. Default size of the buffer is 32kB.
let f = await Deno.open("/etc/passwd");
const iter = Deno.iter(f, {
bufSize: 1024 * 1024
});
for await (const chunk of iter) {
console.log(chunk);
}
f.close();
迭代器使用固定大小的内部缓冲区来提高效率; 它在每次迭代时都返回该缓冲区的视图。 因此,调用方有责任在需要时复制缓冲区的内容。 否则,下一次迭代将覆盖先前返回的块的内容。
Iterator uses an internal buffer of fixed size for efficiency; it returns a view on that buffer on each iteration. It is therefore caller's responsibility to copy contents of the buffer if needed; otherwise the next iteration will overwrite contents of previously returned chunk.