Deno.iterSync(r: ReaderSync, options?: { bufSize: number }) : IterableIterator<Uint8Array>
将ReaderSync r变成迭代器。
Turns a ReaderSync, r, into an iterator.
r : ReaderSync
options ?: { bufSize: number }
r : ReaderSync
options ?: { bufSize: number }
IterableIterator<Uint8Array>
IterableIterator<Uint8Array>
let f = Deno.openSync("/etc/passwd");
for (const chunk of Deno.iterSync(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.iterSync(f, {
bufSize: 1024 * 1024
});
for (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.