Context
In my journey into Typescript, I was advised that blocking calls should never be done inside async code.
I'm also using generators, since they make directory traversal easy and avoid running out of stack space.
However, when I combine async code (in this case: readdir) with generators, the compiler complains that yield can only be used inside generators, leading me to think that the compiler is not able to combine closures, generators and async code, all in one go.
function *yyyymmGenerator(dir: string, props: Props) {
const fs = require("fs");
const yyyy = props.range.getUTCFullYear().toString();
const mm = props.range.getUTCMonth().toString();
const start = `${yyyy}-${mm}`;
const files = fs.readdir(dir, function(err, files) {
for (let i = 0; i < files.length; i++) {
const file: string = files[i];
if (file.localeCompare(start) >= 0) {
const d = `${dir}/${file}`;
yield file;
}
}
});
}
error TS1163: A 'yield' expression is only allowed in a generator body.
Questions
What would be the recommended best practice in a situation like this?
Would it be OK if I simply consider everything sync, blocking code but "wrap" the call inside a Promise?