I'd like to have a function like this:
export async function* iterateDir(dir: string) {
let list = await fs.readdir(dir); // fs-promise implementation of readdir
for (let file of list) {
yield file;
}
}
Which I would use like:
for (let file in iterateDir(dir)) {
processFile(file);
}
This doesn't work because a function cannot be both async and a generator.
How would I structure the code to achieve the same?
- If I change the
await fs.readdir
to callbacks, I assume the outer for..of loop would not wait. - If I get rid of the generator and the directory is huge,
iterateDir()
will be slow.
For reference: async generator function proposal